2015-03-25 128 views
0

我有一個包含兩個dropdownlist的框。當第二次下拉式更改時,框內容會更改。盒子裏有很多李的內容,我想點擊李李裏的內容。這在頁面加載時首次執行,但是在框內容更改後,我的腳本不能運行。在沒有頁面加載的情況下運行javascript


這是我的代碼(當下拉變化,箱內容的變化):

$.ajax({url: '/Symfony/web/app_dev.php/linknews?s='+target+'&&c='+select, 
      success: function(output) { 
       var list = document.getElementById(news_list); 
       var line = document.getElementById("line"); 
       $(list).empty(); 

      var newss = JSON.parse(output); 
      newss.forEach(function(entry){ 

      var link = document.createElement("a"); 
      var linkcontent = document.createTextNode(entry["title"]); 
      link.appendChild(linkcontent); 
      link.title = entry["title"]; 
      link.href = entry["address"]; 

      var div = document.createElement("div"); 
      div.innerHTML += '&nbsp'; 
      div.setAttribute("class" , "news-bottom"); 

      var li = document.createElement("li"); 
      li.appendChild(link); 
      li.appendChild(div); 

      list.appendChild(li); 
      $(line).show(); 
      });  
     } 
}); 

和代碼給我看李內容:

$(document).ready(function($) 
     $("#news-list li a").click(function() { 
     var name = this.getAttribute('href'); 
     alert(name); 
})); 

我不不知道盒子內容何時改變,爲什麼我的腳本不能運行?

+2

您是否考慮添加一些代碼以向我們展示您迄今爲止所做的工作? – D4V1D 2015-03-25 07:59:59

回答

0

我的問題有兩個方面:

第一: 跨瀏覽器onload事件和後退按鈕,並回答: cross-browser

第二: 我應該使用點擊上: $(document).ready(function(){

$('#news-list').on('click','a',function(){ 

    alert($(this).attr('href')); // Get the ref attribute from the "a" element 
}); 
$(window).bind("unload", function() {}); 

});

注意: 在jQuery 1.7版中引入了方法。

相關問題