我有一个盒子,里面有两个下拉列表。当第二个下拉列表更改时,框内容更改。盒子里有很多李,我想当点击李时得到李的内容。这是第一次在页面加载时执行,但在框内容更改后,我的脚本不会运行。
这是我的代码(当下拉列表和框内容发生变化时):
$.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 += ' ';
div.setAttribute("class" , "news-bottom");
var li = document.createElement("li");
li.appendChild(link);
li.appendChild(div);
list.appendChild(li);
$(line).show();
});
}
});
以及向我展示li内容的代码:
$(document).ready(function($)
$("#news-list li a").click(function() {
var name = this.getAttribute('href');
alert(name);
}));
我不知道盒子内容什么时候变了,为什么我的脚本不运行?!
我的问题有两个方面:
第一个:跨浏览器加载事件和后退按钮,然后回答:跨浏览器
秒:我应该使用on而不是单击:$(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中引入的。