此代码假设向每个 a
元素添加一个 onClick 事件,以便单击该元素会将页面内容动态加载到 DIV 中。
现在,我已经做到了这一点 - 它将添加 onClick 事件,但如何加载动态内容?
$(document.body).ready(function () {
$("li.cat-item a").each(function (i) {
this.setAttribute('onclick','alert("[load content dynamically into #preview]")');
});
});
谢谢。
最佳答案
$(document.body).ready(function () {
$("li.cat-item a").click(function (e) {
$('#this-is-your-target-div').load(this.href);
e.preventDefault();
});
});
请参阅此处 jQuery ajax 函数的文档:http://api.jquery.com/category/ajax/ .
关于javascript - 如何设置加载动态内容的 onClick 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2523565/