2015-03-25 125 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版中引入了方法。

相关问题