简体   繁体   English

jQuery触发on('click')动态加载的内容

[英]jquery trigger on('click') of dynamically loaded content

I'm writing a webpage that uses an image map. 我正在写一个使用图像地图的网页。 The image and map are loaded dynamically. 图像和地图是动态加载的。 If I click on the area element new content is loaded. 如果单击区域元素,则会加载新内容。 Also the url changes its hash (eg. index.php#somepage). 网址也会更改其哈希值(例如index.php#somepage)。 I have three layers of pages, the main layer (homepage) has it's own image with a map (index.php), the second layer offers a new image + map (index.php#somepage) and the third layer opens an overlay on the second layer, therefore changing the hash (index.php#somepage_somesubpage). 我有三层页面,主层(主页)有它自己的带有地图的图像(index.php),第二层提供了新的图像+地图(index.php#somepage),第三层打开了一个覆盖图第二层,因此更改了哈希值(index.php#somepage_somesubpage)。

I now want to be able to send someone a link to index.php#somepage_somesubpage. 现在,我希望能够向某人发送指向index.php#somepage_somesubpage的链接。 So I slice the hash and trigger the click-method of the imagemap on the first level to load index.php#somepage when the page is loaded. 因此,我对哈希进行了切片,并在第一级上触发了图像映射的单击方法,以在加载页面时加载index.php#somepage。 I added a callback to that, calling the desired click-method of the now updated imagemap. 我为此添加了一个回调,调用了现在更新的imagemap的所需单击方法。 This does not work for some reason I can't figure out. 由于我无法弄清某些原因,这无法正常工作。 I am able to open index.php#somepage, but when I enter index.php#somepage_somesubpage I end up getting the same result. 我可以打开index.php#somepage,但是当我输入index.php#somepage_somesubpage时,我最终会得到相同的结果。

Here is the code of $(document).ready: 这是$(document).ready的代码:

var cont = $('#content');

$( document ).ready(function() {
    cont.load('pages/home.php', function(responseTxt,statusTxt,xhr){
        if(statusTxt=='error')
        {
            cont.load('404.php');
        }
        lineparser('#content'); //Perform some fancy stuff on the page

        var hash = location.hash.replace('#', '');

        if (hash != ''){
            if(hash.indexOf('_') > 0)
            {
                //open page with content

                $('area[href~="#' + hash.slice(0, hash.indexOf('_')) + '"]').first().trigger("click", function(){
                    $('area[href~="#' + hash + '"]').first().trigger("click");
                });
            }
            else{
                //open menu page

                $('area[href~="#' + hash + '"]').first().trigger("click");
            }
        }
    }); 
});

I solved the problem like this: 我解决了这样的问题:

$('area[href~="#' + hash.slice(0, hash.indexOf('_')) + '"]').first().trigger("click", [hash]);

Then I added the following: 然后我添加了以下内容:

$(document.body).on('click', "map area", function(e, schachteln){
    ...
    if(schachteln){
        $('area[href~="#' + schachteln + '"]').first().trigger("click");
    }
}

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

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