运行 javascript 没有页面加载
Run javascript without page load
我有一个包含两个下拉列表的框。当第二个下拉列表更改时,框内容会更改。框包含带有很多 li 的 ul,我希望在单击 li 时获取 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();
});
}
});
以及向我展示 li 内容的代码:
$(document).ready(function($)
$("#news-list li a").click(function() {
var name = this.getAttribute('href');
alert(name);
}));
我不知道盒子内容什么时候变了,为什么我的脚本没有运行?!
我的问题有两个方面:
第一:
跨浏览器 onload 事件和 Back button ,并回答:
cross-browser
秒:
我应该使用 on 而不是 click:
$(文档).ready(函数(){
$('#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版本中引入。
我有一个包含两个下拉列表的框。当第二个下拉列表更改时,框内容会更改。框包含带有很多 li 的 ul,我希望在单击 li 时获取 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();
});
}
});
以及向我展示 li 内容的代码:
$(document).ready(function($)
$("#news-list li a").click(function() {
var name = this.getAttribute('href');
alert(name);
}));
我不知道盒子内容什么时候变了,为什么我的脚本没有运行?!
我的问题有两个方面:
第一: 跨浏览器 onload 事件和 Back button ,并回答: cross-browser
秒: 我应该使用 on 而不是 click: $(文档).ready(函数(){
$('#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版本中引入。