2013-09-25 47 views
0

我在寫一個使用圖像映射的網頁。圖像和地圖是動態加載的。如果我點擊區域元素,新的內容被加載。此外,網址更改其散列(例如,index.php#somepage)。我有三層頁面,主圖層(主頁)具有自己的帶有地圖的圖像(index.php),第二層提供了新的圖像+地圖(index.php#somepage),第三層打開覆蓋層第二層,因此改變散列(index.php#somepage_somesubpage)。jquery觸發動態加載的內容('點擊')

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

這裏是$(文件)。就緒的代碼:

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"); 
      } 
     } 
    }); 
}); 
+0

觸發()jquery方法不接受任何回調作爲第二個參數,只有一個對象或額外數據的數組傳遞給事件處理程序http://api.jque ry.com/trigger/ –

+0

那麼,我可以使用.click()嗎?如果沒有,看到簡單地將線條放在一起並不會有幫助,我該怎麼辦? – Urinprobe

回答

0

我解決了這樣的問題:

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