Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jQuery中插入后更新DOM_Jquery_Ajax - Fatal编程技术网

在jQuery中插入后更新DOM

在jQuery中插入后更新DOM,jquery,ajax,Jquery,Ajax,假设我使用jQuery将新内容加载到特定的DIV元素中。如果我现在想从DIV元素内部捕获事件,我假设DOM必须以某种方式更新?处理这个问题的最好方法是什么 编辑:下面是我的意思的一个例子: <html> <head> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script> $(document).ready(f

假设我使用jQuery将新内容加载到特定的DIV元素中。如果我现在想从DIV元素内部捕获事件,我假设DOM必须以某种方式更新?处理这个问题的最好方法是什么

编辑:下面是我的意思的一个例子:

<html>
  <head>
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script>
      $(document).ready(function(){
        $(".edit").click(function(){
          $(this).parent().load("edit");
        });
        $(".view").click(function(){
          $(this).parent().load("view");
        });
      });
    </script>
  </head>
  <body>
    <div class='someid'>
      <input type='submit' class='edit' value='Edit'>
    </div>
  </body>
</html>
文件“视图”包含

<input type='submit' class='edit' value='Edit'>

如果你尝试一下,你会发现当你第一次按下编辑按钮时,按钮会变为查看,但之后就不再改变了。这是为什么?

jQuery使用“”事件来处理动态添加的DOM元素中发生的事情,而无需在内容更新后手动添加处理程序。

如下使用:

 $(".view").live("click",function(){
      $(this).parent().load("view");
    });

自从jQuery 1.7.x以来,.live()方法已被弃用。使用.on()附加事件处理程序。如果您使用的是旧版本,则应优先使用.delegate(),而不是.live()

如帮助中所述,格式没有发生很大变化,如本例所示:

$(选择器).live(事件、数据、处理程序);//jQuery 1.3+
$(文档).delegate(选择器、事件、数据、处理程序);//jQuery 1.4.3+
$(文档).on(事件、选择器、数据、处理程序);//jQuery 1.7+


因此,自1.7.x以来,委托活动都被上的取代了

如果将新内容加载到DIV元素中,这与更新DOM是一样的,不是吗?我的意思是如果使用例如$(this).load('getcontent'))而且内容包含了新的DIV元素和以前没有定义过的新id,我似乎无法用jquery选择这些新元素。我已经在我的原始帖子中添加了一个示例来说明问题所在
 $(".view").live("click",function(){
      $(this).parent().load("view");
    });