javascript - 如何添加一个单独的 html 文件

标签 javascript html dom-events

我想在一个 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/

相关文章:

javascript - 防止固定标题重叠内容

html - 为什么输入的默认最大长度为 524288?

javascript - 移动设备上的 Google Chrome 在文件输入时触发额外不需要的 MouseEvent

javascript - 事件捕获未按预期运行

javascript - 使用javascript更改div的顺序

javascript - iframe 内的 Google map 未加载

Javascript:编写 if/else 语句的更好方法?也许使用数组?

html - 背景大小 :contain on my hero image 遇到一些问题

html - CSS 工具提示仅适用于 FF

javascript - 散布 KineticJS 和 jQuery 事件