Jquery change()不起作用

Hac*_*ker 0 javascript jquery

大家好,我是jquery的新手.我面临着更改函数I jquery的问题

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>

  $("input").change(function(){
    alert("The text has been changed.");
  });
</script>
</head>
<body>

<input type="text">
<p>Write something in the input field, and then press enter or click outside the field.</p>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我在文本框外面按下它时,我没有收到警报框.请帮我解决这个问题:D

Aru*_*hny 7

您正在尝试在将元素添加到dom之前向元素添加更改处理程序.您可以使用dom ready回调来延迟脚本执行,直到所有元素都加载到dom

jQuery(function($){
  $("input").change(function(){
    alert("The text has been changed.");
  });
})
Run Code Online (Sandbox Code Playgroud)

演示:小提琴