通过ajax加载返回到主要内容后,函数onload
没有运行。
我能理解为什么,但我怎样才能让它在那种情况下运行?
<script type="text/javascript">
onload = function() {
if (!document.getElementsByTagName || !document.createTextNode) return;
var rows = document.getElementById('chat').getElementsByTagName('tr');
for (i = 0; i < rows.length; i++) {
rows[i].onclick = function() {
$("#chat_main").load("chat", {
m: this.id,
ajax: 1 //here we are loading another page
});
}
}
}
</script>
<script>
function return_to_main() {
$("#chat_main").load("chat", {
ajax: 1 //here we trying to load back main page
});
}
</script>
P.S. return_to_main()
绑定(bind)在 input type="button"
最佳答案
您正在绑定(bind)到 window.onload 调用。它不会在每次更新页面内容时神奇地被调用。它只被调用一次。每次要运行代码时都需要调用一个函数。因此,当 Ajax 调用完成时,您将触发它。
但是您正在使用 jQuery,所以请使用它。
没有理由需要绑定(bind)到表中的每一行。使用事件委托(delegate)。现在,当内容发生变化时,您仍将绑定(bind)事件。
$( function () { //document ready
var chatMain = $("#chat_main");
chatMain.on("click", "table tbody tr", function () { //listen for clicks on table row
chatMain.load("chat",
{
m: this.id,
ajax: 1 //here we are loading another page
}
);
});
});
关于ajax加载后的Javascript onload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30033235/