我想在一个 html 中绑定(bind)以下脚本和表单代码,我该怎么做? 我认为这段代码会在没有任何显示时给我一个空白值?
<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;
}
});
});
最佳答案
嘿,你可以试试这个:-
<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>
关于javascript - 如何添加一个单独的 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17361184/