简体   繁体   English

jQuery Click函数未在动态数据上调用

[英]Jquery click function not being called on dynamic data

I've gone through the existing questions and can't find a solution to my problem, so here goes... 我已经解决了现有的问题,但找不到解决问题的方法,所以这里...

I am trying to implement a carousel inside of a modal, but the click function is not being called when I activate the modal. 我正在尝试在模式内部实现轮播,但是在激活模式时未调用click函数。 The HTML is generated dynamically, so I'm think my problem is how I'm incorporating the javascript. HTML是动态生成的,所以我认为我的问题是如何合并javascript。

I should mention this is what I'm trying to implement: http://www.bootply.com/alcaraz/QxGeDFsS8G# 我应该提到这就是我要实现的目标: http : //www.bootply.com/alcaraz/QxGeDFsS8G#

Here is my my HTML that is being loaded dynamically 这是我的动态加载的HTML

<div class="container" id="photography">
<div class="row">
    <div class="col-lg-3 col-md-3 col-sm-3">
        <a data-toggle="modal" data-target="#modal-gallery" href="#">
            <img src="img/img1.jpg" class="thumbnail img-responsive" id="image-1">
        </a>
        </br>
        <a data-toggle="modal" data-target="#modal-gallery" href="#">
            <img src="img/img1.jpg" class="thumbnail img-responsive">
        </a>
    </div>
    <div class="col-lg-3 col-md-3 col-sm-3">
        <a data-toggle="modal" data-target="#modal-gallery" href="#">
            <img src="img/img1.jpg" class=" thumbnail img-responsive" id="image-2">
        </a>
    </div>
    <div class="col-lg-3 col-md-3 col-sm-3">
        <a data-toggle="modal" data-target="#modal-gallery" href="#">
            <img src="img/img1.jpg" class="thumbnail img-responsive" id="image-3">
        </a>
    </div>
</div><!--Row-->

<div class="hidden" id="image-repo">
<!-- #image-1 -->
    <div class="item" id="image-1">
        <img class="thumbnail img-responsive" title="Image 11" src="img/img1.jpg">
    </div>
    <div class="item" id="image-1">
        <img class="thumbnail img-responsive" title="Image 12" src="img/img1.jpg">
    </div>
    <div class="item" id="image-1">
        <img class="thumbnail img-responsive" title="Image 13" src="img/img1.jpg">
    </div>

    <!-- #image-2 -->
    <div class="item" id="image-2">
        <img class="thumbnail img-responsive" title="Image 21" src="http://dummyimage.com/600x350/2255EE/969696">
    </div>
    <div class="item" id="image-2">
        <img class="thumbnail img-responsive" title="Image 21" src="http://dummyimage.com/600x600/2255EE/969696">
    </div>
    <div class="item" id="image-2">
        <img class="thumbnail img-responsive" title="Image 23" src="http://dummyimage.com/300x300/2255EE/969696">
    </div>   

    <!-- #image-3-->
    <div class="item" id="image-3">
        <img class="thumbnail img-responsive" title="Image 31" src="http://dummyimage.com/600x350/449955/FFF">
    </div>
    <div class="item" id="image-3">
        <img class="thumbnail img-responsive" title="Image 32" src="http://dummyimage.com/600x600/449955/FFF">
    </div>
    <div class="item" id="image-3">
        <img class="thumbnail img-responsive" title="Image 33" src="http://dummyimage.com/300x300/449955/FFF">
    </div> 

And here is my Javascript 这是我的Javascript

$("document").ready(function(){
 $('.stop-this-link').on('click', false);
 $("#About").on("click", clickAbout);
 $("#Projects").on("click", clickProjects);
 $("#Photography").on("click", clickPhotography);
 $("#Film").on("click", clickFilm);
 $("#Resume").on("click", clickResume);
 $("#Contact").on("click", clickContact);

});


function clickAbout(evt){
 $("#portfolio").load("aboutMe.html");
}

function clickProjects(evt){
 $("#portfolio").html("Clicked Projects");
}


function clickResume(evt){
 $("#portfolio").html("Clicked Resume");
}


function clickPhotography(evt){
 $("#portfolio").load("photography.html");
         /* activate the carousel */
 $("#modal-carousel").carousel({interval:false});

 /* change modal title when slide changes */
 $("#modal-carousel").on("slid.bs.carousel", function () {
   $(".modal-title").html($(this).find(".active img").attr("title"));
 })

/* when clicking a thumbnail */
//$(".portfolio .photography").on('click', ".row .thumbnail", function(event){
 //$(document.body).on('click', '.row .thumbnail', function(event){
 $(".row .thumbnail").click(function(){
     var content = $(".carousel-inner");
     var title = $(".modal-title");

     content.empty();  
     title.empty();

     var id = this.id;  
     var repo = $("#img-repo .item");
     var repoCopy = repo.filter("#" + id).clone();
     var active = repoCopy.first();

     active.addClass("active");
     title.html(active.find("img").attr("title"));
     content.append(repoCopy);

     // show the modal
     //$("#modal-gallery").modal("show");
 });

}

function clickFilm(evt){
 $("#portfolio").load("film.html");
}

function clickContact(evt){
 $("#portfolio").load("contact.php");
}

Finally, here is the static HTML that the dynamic content is loaded into. 最后,这是将动态内容加载到其中的静态HTML。

<div class="col-md-9 col-lg-9 col-xs-9 col-sm-9" id="portfolio"></div> <!--Portfolio!-->
 <div id="modal-gallery" class="modal fade" role="dialog">
     <div class="modal-dialog">
         <div class="modal-content">
             <div class="modal-header">
                 <h3 class="modal-title"></h3>
             </div><!--Modal Header-->
             <div class="modal-body">
                 <div id="modal-carousel" class="carousel">
                     <div class="carousel-inner">

                     </div><!--Carousel-Inner-->
                     <a class="carousel-control-left" href="#modal-carousel" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
                     <a class="carousel-control-right" href="#modal-carousel" data-slide"prev"><i class="glyphicon glyphicon-chevron-right"></i></a>
                 </div><!--Carousel-->
             </div><!--Modal Body-->
             <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
             </div><!--Footer-->
         </div><!--modal content-->
     </div><!--Modal Dialog-->
 </div><!--modal-gallery-->

You need to propagate the event when dynamic elements are added to the DOM: 将动态元素添加到DOM时,您需要传播事件:

$("body").on("click", '#About', clickAbout);

Event delegation is basically attaching the event to a parent container (for the sake of the example i've used body, but you might want to use a closer parent in order to make it more performant). 事件委托基本上是将事件附加到父容器(出于示例的目的,我使用了body,但是您可能希望使用更近的父对象,以使其性能更高)。 This parent will have the event and it will know when a new child is added to it, and delegate the event to that given child. 该父对象将拥有该事件,并且它将知道何时添加新的孩子,并将事件委托给该给定的孩子。

You need to use event delegation for this. 您需要为此使用事件委托。 You are binding events on page load (inside your document ready function), but the elements don't exist in the page yet. 您正在页面加载时绑定事件(在文档就绪功能内部),但是页面中尚不存在这些元素。 You should do something like this: 您应该执行以下操作:

$('body').on('click', '#About', clickAbout);

This will listen for click events on the body tag and then see if they match the provided selector. 这将侦听body标签上的click事件,然后查看它们是否与提供的选择器匹配。 Read more about event delegation here: http://api.jquery.com/on/ 在此处阅读有关事件委托的更多信息: http : //api.jquery.com/on/

A more efficient way to accomplish this in your particular use case would be to set the onclick attribute of the elements that you are adding dynamically. 在您的特定用例中,一种更有效的方法是设置要动态添加的元素的onclick属性。 For example: 例如:

<div id="About" onclick="clickAbout">

Also, you have some issues with your HTML. 另外,您的HTML也有一些问题。 You cannot assign the same ID to multiple elements as you have. 您不能将相同的ID分配给多个元素。 You have three divs with the id "image-1", for example. 例如,您具有ID为“ image-1”的三个div。 IDs must be unique and only one element with a given ID should exist within the page. ID必须是唯一的,并且页面中只能存在一个具有给定ID的元素。 Otherwise, you'll run into all sorts of problems. 否则,您将遇到各种各样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM