Javascript 如何在jQuery html()之后使用ID选择器

Javascript 如何在jQuery html()之后使用ID选择器,javascript,jquery,html,Javascript,Jquery,Html,尝试理解为什么在多次单击后不生成函数警报(“hello”),这是一件令人惊讶的事情。。。有什么方法可以做这个函数是执行的吗 请注意,在使用包含id“press”in按钮的html()进行更新后,该选项不起作用 有什么想法吗?见: JavaScript: $(document).ready(function (){ $("#press").click(function() { $("#relation-states").html('<select id="

尝试理解为什么在多次单击后不生成函数
警报(“hello”)
,这是一件令人惊讶的事情。。。有什么方法可以做这个函数是执行的吗

请注意,在使用包含id“press”in按钮的
html()
进行更新后,该选项不起作用

有什么想法吗?见:

JavaScript:

  $(document).ready(function (){
      $("#press").click(function() {
          $("#relation-states").html('<select id="state" name="state"> <option value="Texas">Texas</option> </select><button id="press" type="button" title="" aria-haspopup="true" style="width: 175px;"><span>Select an item</span></button>');;
          alert("hello");
      });
  });
$(文档).ready(函数(){
$(“#按”)。单击(函数(){
$(“#关系状态”).html('Texas Select an item');;
警惕(“你好”);
});
});
HTML:


纽约
选择一个项目

您将整个
div的内容替换为id“relationship states”,这也将删除事件处理程序(因为已附加处理程序的DOM节点将被删除)。如果现在或将来要将事件处理程序绑定到符合选择器的所有元素,请签出:

使用时,将删除该元素的所有子元素,并将其替换为新的子元素。这意味着附加到这些子级的任何事件处理程序都将丢失。您可以通过只替换您真正需要的:

$('#state').html('<option value="Texas">Texas</option>');
$('#state').html('Texas');

谢谢,伙计。起初,我试图在再次单击后运行“alert()”。。。我的困难就在于此。
  $("#press").live("click", function() {
      $("#relation-states").html('<select id="state" name="state"> <option value="Texas">Texas</option> </select><button id="press" type="button" title="" aria-haspopup="true" style="width: 175px;"><span>Select an item</span></button>');;
      alert("hello");
  });
$("#relation-states").delegate("#press", "click", function() {
          $("#relation-states").html('<select id="state" name="state"> <option value="Texas">Texas</option> </select><button id="press" type="button" title="" aria-haspopup="true" style="width: 175px;"><span>Select an item</span></button>');;
          alert("hello");
}
$('#state').html('<option value="Texas">Texas</option>');