Javascript 对象没有方法';焦点';在谷歌浏览器中
我在HTML页面中输入:Javascript 对象没有方法';焦点';在谷歌浏览器中,javascript,jquery,google-chrome,onfocus,Javascript,Jquery,Google Chrome,Onfocus,我在HTML页面中输入: <input id="Name" name="Name" tabindex="0" type="text" value="Name:" /> 我使用jQuery编写javascript来绑定事件“onfocus”和“onblur”的处理程序: 变量名称=$(“#名称”); name.focus(函数(){if($(this.val()=“name:”)$(this.val('');}); name.blur(函数(){if($(this.val()=
<input id="Name" name="Name" tabindex="0" type="text" value="Name:" />
我使用jQuery编写javascript来绑定事件“onfocus”和“onblur”的处理程序:
变量名称=$(“#名称”);
name.focus(函数(){if($(this.val()=“name:”)$(this.val('');});
name.blur(函数(){if($(this.val()==“”)$(this.val)(“name:”;});
所以,当用户选择输入时,它的值应该为空。若用户未在输入中输入文本并更改焦点,则输入的值将返回“名称:”
它在Firefox、Internet Explorer和Opera中运行良好,但在Google Chrome中不起作用。此外,chrome调试器中存在错误:“UncaughtTypeError:Object[Object Object]没有“focus”方法”
如何在Google Chrome中使用jQuery绑定处理程序?
谢谢 在这种情况下,Chrome不喜欢将变量命名为“name”。以下作品
<script type="text/javascript">
var name2 = $("#Name");
name2.focus(function () {if ($(this).val() == "Name:") $(this).val('');});
name2.blur(function () {if ($(this).val() == "") $(this).val("Name:");});
</script>
变量名称2=$(“#名称”);
name2.focus(函数(){if($(this).val()=“Name:)$(this.val('');});
name2.blur(函数(){if($(this).val()==“”)$(this.val(“Name:”;});
脚本标记在输入标记之后吗?页面上是否有id=“Name”的其他元素?它适用于我在OS X上的Chrome 22。在这里工作正常。您是否将其包含在document.ready call中?在Win XP和Win 7中的Google Chrome 21中检测到错误。document.ready call中不包含脚本。它放在html前面的末尾
<script type="text/javascript">
var name2 = $("#Name");
name2.focus(function () {if ($(this).val() == "Name:") $(this).val('');});
name2.blur(function () {if ($(this).val() == "") $(this).val("Name:");});
</script>