2012-08-15 115 views
0

我有一個問題,我爲難我的代碼無法正常工作,我嘗試在單擊列表項錨點標記時將PHP文件加載到div中。Jquery.Load on click not working

代碼:

<script type="text/javascript"> 
$(document).ready(function() { 


//I have these 2 functions aswell that DO work 

$(".itm1 a").click(function(evt) { 
    var page = $(this).attr('href'); 
    $("#page1").load(page); 
    evt.preventDefault(); 
    }); 

    $(".footnav a").click(function(evt) { 
    var page3 = $(this).attr('href'); 
    $("#page1").load(page3); 
    evt.preventDefault(); 
    }); 
// --------------------------- 

    $(".needslist a").click(function(evt) { 
    var page4 = $(this).attr('rel'); 
    $("#page1").load(page4) 
    evt.preventDefault(); 
    }); 



}); 
</script> 


<ul> 
<li class="needslist"><span><a rel="/ceos.php" href="#">Are you a CEO SPEECH-MAKER or ENTREPRENEURIAL VISIONARY?</a></span><br /> 
      who wants the best advice and counsel from an expert coach for your own speeches and leadership events that include speaking to investors, stakeholders, business collaborators, board of directors, conferences and/or the media?</li><br /> 

</ul> 

誰能提供一個explination爲什麼它不工作?

編輯: 好的夥計們,所以我發現一旦我加載一個div的ajax,該內容變得無法處理jQuery。這是我的問題....如何執行jquery腳本加載的內容???? !!!

+1

是什麼'#page1'這裏?在加載 – 2012-08-15 19:00:09

+1

之後,你還有一個輸入錯誤的';'你是否有任何錯誤? – MrOBrian 2012-08-15 19:03:54

+0

#page1是試圖將php文件加載到...並沒有錯誤 – onei0120 2012-08-15 19:05:17

回答

1

找到了解決辦法:

當你將Ajax內容,你不能對加載的內容執行任何腳本,我的繼承人的解決方案:

$(".needslist a").live("click", function(evt) { 
    var page4 = $(this).attr('rel'); 
    $("#page1").load(page4); 
    evt.preventDefault(); 
    }); 
+0

不推薦使用'live()',它有許多缺點和問題。使用'on()'這是自jQuery 1.7以來推薦的方法,或者使用'delegate()'前1.7來綁定動態元素,'bind()'使用靜態元素。查看關於'live()'的jQuery文檔以獲得問題列表:http://api.jquery.com/live/ – Nope 2012-08-24 09:19:50

+0

當我使用on()時,它似乎打破了我的頁面上的所有代碼 – onei0120 2012-08-28 18:41:39