簡體   English   中英

jQuery觸發on('click')動態加載的內容

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

我正在寫一個使用圖像地圖的網頁。 圖像和地圖是動態加載的。 如果單擊區域元素,則會加載新內容。 網址也會更改其哈希值(例如index.php#somepage)。 我有三層頁面,主層(主頁)有它自己的帶有地圖的圖像(index.php),第二層提供了新的圖像+地圖(index.php#somepage),第三層打開了一個覆蓋圖第二層,因此更改了哈希值(index.php#somepage_somesubpage)。

現在,我希望能夠向某人發送指向index.php#somepage_somesubpage的鏈接。 因此,我對哈希進行了切片,並在第一級上觸發了圖像映射的單擊方法,以在加載頁面時加載index.php#somepage。 我為此添加了一個回調,調用了現在更新的imagemap的所需單擊方法。 由於我無法弄清某些原因,這無法正常工作。 我可以打開index.php#somepage,但是當我輸入index.php#somepage_somesubpage時,我最終會得到相同的結果。

這是$(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");
            }
        }
    }); 
});

我解決了這樣的問題:

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

然后我添加了以下內容:

$(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