我有一个带有两个下拉列表的框。第二次下拉更改时,框内容会发生变化包含很多li的ul,我想点击li get li的内容。这是第一次在页面加载时,但在内容更改后,我的脚本无法运行。
这是我的代码(当下拉列表更改和框内容更改时):
$.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();
});
}
});
以及向我展示内容的代码:
$(document).ready(function($)
$("#news-list li a").click(function() {
var name = this.getAttribute('href');
alert(name);
}));
我不知道盒子内容何时更改,为什么我的脚本不能运行?!
答案 0 :(得分:0)
我的问题有两个方面:
<强>第一强> 跨浏览器onload事件和Back按钮,并回答: cross-browser
<强>第二强> 我应该使用而不是点击: $(文件)。就绪(函数(){
$('#news-list').on('click','a',function(){
alert($(this).attr('href')); // Get the ref attribute from the "a" element
});
$(window).bind("unload", function() {});
});
注意: 方法on在jQuery 1.7版中引入。