jQuery代码中的追加并单击功能? [英] An append in jquery code and click function?

查看:69
本文介绍了jQuery代码中的追加并单击功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的以下test.html文件

This is my following test.html file

<!DOCTYPE html>
    <html>
      <head>
          <style>
              p { 
                  background:yellow; 
              }
              .content{
                  width:40px;
                  margin:0px 30px;
              }
          </style>
          <script src="http://code.jquery.com/jquery-latest.js"></script>
          <script>
              $(document).ready(function(){
                  $('#test').click(function(){
                     // alert("test");
                     var htmlData='<div class="resizable" ><div class="content">test</div></div>';
                     $('.container').append(htmlData);
                  });
                  $('.content').click(function(){
                     alert("test");
                  });
              });
          </script>
     </head>
     <body>
         <div class="container">
             <div class="resizable" >
                 <div class="selected content">Time</div>
                 <button type="button" id="test">Click Me!</button>
             </div>
         </div>
    </body>
</html>

当我单击按钮时,添加了一个div,我也有写了click函数的类内容,称为alert.content类的jquery click函数会调用内容类,后者是硬编码,但没有内容类别,由buttton的click功能附加.

When i click on the button a div is appended i have also class content on which click function is written which called alert.The jquery click function for content class does calls to the the content class which is hardcode but no for the content class which is appended by the click function of buttton.

推荐答案

您需要对动态创建的DOM元素使用实时功能-

You need to use live function for DOM elements created dynamically - http://api.jquery.com/live/

$('.content').live('click', function(){
   alert("test");
});

这篇关于jQuery代码中的追加并单击功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆