将jquery脚本应用于附加数据

时间:2014-11-28 15:37:27

标签: javascript jquery html json

我有一个附加一些json数据的脚本。在该附加数据中,我在链接上有一个类,应该停止该链接转到url。问题是我认为jquery不适用于附加数据,我不知道如何使其工作。这是我的代码 附加数据:

<script>
$(".filtru").click(function(e){
    e.preventDefault();
    var href = $(this).attr(\'href\');
    $.getJSON(href, function(data) {
                        $("#rest").empty();


                        $.each(data, function(key, val) {
                                $("#rest").append(\'<li> <a href="http://carulcuflori.rezerv.city"><div class="foto_local span3"><h2>\'+ val.denumire + \'</h2></div></a><div class="continut1 span5">\
    <div class="continut2">\
    <ul>\
    <li><span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span> ' +val.address +\'</li>\
    <li><span class="glyphicon glyphicon-time" aria-hidden="true"></span> L-V: 06-01&nbsp&nbspS: 06-23&nbsp&nbspD: Inchis</li>\
    <li><span class="glyphicon glyphicon-phone-alt" aria-hidden="true"></span> 0351.44.42.43 / 0767.226.224 </li>\
    <li><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> carulcuflori@yahoo.com </li>\
    </ul>\
    </div></a>\
    </div>\
     <div class="navigation span5">\
     <div id="pagenav" class="menu_mic">\
     <ul class="nav menu">\
     <li class="item-466 color7 home1" data-open-text="Inchide"><a href="http://rezerv.city/engine/app/maps.php?id=\' + val.id + \'" class="locatie"><img src="images/locatie.png" alt="Locatie"><span class="image-title">Locatie</span> </a></li>\
     <li class="item-466 color8 home1"  data-open-text="Inchide"><a href="#"><img src="images/galerie.png" alt="Galerie foto"><span class="image-title">Galerie Foto</span> </a></li>\
     <li class="item-466 color9 home1"  data-open-text="Inchide"><a href="#"><img src="images/facebook.png" alt="Facebook"><span class="image-title">Facebook</span> </a></li>\
     </ul>\
     </div>\
     </div></li>\');
                        });
                        $(\'#rest\').paginate({itemsPerPage: 2});

                });
});
</script>

以下是应该停止类locatie转到url的脚本

<script>
$(".locatie").click(function(e){
    e.preventDefault();
    var href = $(this).attr(\'href\');
    $.getJSON(href, function(data) {
$(\'#myModal2\').modal(\'show\');
    $(\'.modal-body\').html(data.map);

     });

      });
</script>

1 个答案:

答案 0 :(得分:2)

Groetjes。

您不能以这种方式对动态创建的元素使用“click”。 我用的是:

$('body').on('click','.locatie',function(e){
  e.preventDefault();
   var href = $(this).attr('href');
    $.getJSON(href, function(data) {
   $('#myModal2').modal('show');
    $('.modal-body').html(data);

     });

      });