当用户点击链接而不是加载整个新页面时,我通过ajax请求加载新页面的HTML数据(还有一个查询字符串,我让服务器每次都不发送导航栏数据)从ajax请求中得到的数据然后我通过DOMParser来允许我从div获取id为“content”的内容并替换当前文档的“context”div的innerHTML。
通过此方法执行请求后,虽然newDOM中的任何脚本标记在放入内容div后都不会运行。此外,它似乎也在newDOM中运行,因为如果你有一个脚本在加载时立即编辑文档,那么当你注销newDOM时没有任何效果
AjaxRequest(href, function(data) {
var parser = new DOMParser();
var newDOM = parser.parseFromString(data.responseText, "text/html");
//Setup new title
var title = '';
if (newDOM.getElementsByTagName('title').length > 0 && newDOM.getElementsByTagName('title')[0] !== null) {
title = newDOM.getElementsByTagName('title')[0].innerHTML;
} else {
title = rawhref;
}
document.title = title;
history.pushState({}, title, rawhref);
if (newDOM.getElementById('content') === null) {
//If there is an error message insert whole body into the content div to get full error message
document.getElementById('content').appendChild(newDOM.getElementsByTagName('body')[0]);
} else {
document.getElementById('content').appendChild(newDOM.getElementById('content'));
}
MapDOM();
if (typeof(onPageLoad) == "function") {
onPageLoad();
}
});
注意:变量“rawhref”只是没有?noheader的请求URL,因此用户可以更容易地回溯历史记录。 注意:在任何新的加载之后,我还有一个覆盖任何新标签的功能,以便它可以通过这种方法用于下一个新页面。
此外,如果答案没有使用jQuery,那将更受欢迎。
答案 0 :(得分:1)
有人刚刚回答了这个问题,当我测试它时,他们删除了他们的解决方案....嗯,非常感谢你曾经是谁,对于将来遇到这个问题的人来说,他们展示的是代码,但是我没有时间完全理解它为什么起作用......但我认为可以解决它。
Sub LoopInsert()
Dim tgt, final, rng, val, cell, cell2, cell3 As Range
Worksheets("Sheet1").Activate
Set rng = ActiveSheet.Range("A2", ActiveSheet.Range("A2").End(xlDown))
Set val = ActiveSheet.Range("B2", ActiveSheet.Range("B2").End(xlDown))
Set tgt = ActiveSheet.Range("C2", ActiveSheet.Range("C2").End(xlDown))
For Each cell In rng
For Each cell2 In val
If cell.Value = "Daily Charges" Then
Exit For
For Each cell3 In tgt
cell3.Value = cell2.Value
Exit For
Next
Else
For Each cell3 In tgt
cell3.Value = 0
Exit For
Next
End If
Next
Next
End Sub