1

我想在一个html中绑定下面的脚本和表单代码我该怎么做?我认为当没有显示任何内容时,这段代码会给我一个空白值?

<form method="post" action="contactengine.php">
    <input type="textbox" class="default-value" name="Name" id="Name" value="Full Name">

    <input type="textbox" class="default-value" name="Email" id="Email" value="Email">

    <input type="submit" name="submit" id="submit" value="Submit" class="submit-button" />
</form> 

$('.default-value').each(function() {
    var default_value = $(this).val();
    $(this).css('color', '#746F66'); // this could be in the style sheet instead
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css('color', 'black');
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css('color', '#746F66');
            this.value = default_value;
        }
    });
});
4

2 回答 2

4

嘿,你可以试试这个:-

<script type="text/javascript">
 $(function(){
 $('.default-value').each(function() {
var default_value = $(this).val();
$(this).css('color', '#746F66'); // this could be in the style sheet instead
$(this).focus(function() {
    if(this.value == default_value) {
        this.value = '';
        $(this).css('color', 'black');
    }
});
  $(this).blur(function() {
    if(this.value == '') {
        $(this).css('color', '#746F66');
        this.value = default_value;
    }
  });
});
});
 </script>
<body>
<form method="post" action="contactengine.php">
<input type="textbox" class="default-value" name="Name" id="Name" value="Full Name">

<input type="textbox" class="default-value" name="Email" id="Email" value="Email">

<input type="submit" name="submit" id="submit" value="Submit" class="submit-button" />
</form>
</body> 
于 2013-06-28T09:42:33.453 回答
2

在 HTML 文件的末尾添加此代码段。

<script type="text/javascript">
$(function(){

$('.default-value').each(function() {
    var default_value = $(this).val();
    $(this).css('color', '#746F66'); // this could be in the style sheet instead
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css('color', 'black');
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css('color', '#746F66');
            this.value = default_value;
        }
    });
});

});
</script>
于 2013-06-28T09:16:25.340 回答