【发布时间】:2017-09-27 20:37:30
【问题描述】:
我有一个网页,我的侧边栏链接会导致将“外部”HTML 文档加载到内容 div 中。
但是成功加载并显示后,加载的 HTML 内容并没有出现在 Page Source 中。
无论如何,我现在需要使用 Javascript 函数对该“外部”HTML 文档进行客户端文本搜索。
我的网页如下所示:
搜索文本框和按钮位于 Content Div 的“外部”(以红色为边框)。
并且,当链接的 HTML 文档之一出现在屏幕上时,页面源代码如下所示:
<!-- Page Content -->
<div id="page-content-wrapper" style="border: thick solid #FF0000; height:660px">
<!--Loaded content goes here-->
</div>
请注意,“已加载”的 HTML 文档未显示。
我找到了一个看起来很有希望的 Javascript 函数 findInPage(),但它没有找到“加载”的 HTML 文档及其文本。
// =====================================
function findInPage() {
var str = document.getElementById("ButtonForm").elements["txtSearch"].value;
var n = 0;
var txt, i, found;
if (str == "")
return false;
// Find next occurance of the given string on the page, wrap around to the
// start of the page if necessary.
if (window.find) {
// Look for match starting at the current point. If not found, rewind
// back to the first match.
if (!window.find(str)) {
while (window.find(str, false, true))
n++;
} else {
n++;
}
// If not found in either direction, give message.
if (n == 0)
alert("Not found.");
} else if (window.document.body.createTextRange) {
txt = window.document.body.createTextRange();
// Find the nth match from the top of the page.
found = true;
i = 0;
while (found === true && i <= n) {
found = txt.findText(str);
if (found) {
txt.moveStart("character", 1);
txt.moveEnd("textedit");
}
i++;
}
// If found, mark it and scroll it into view.
if (found) {
txt.moveStart("character", -1);
txt.findText(str);
txt.select();
txt.scrollIntoView();
n++;
} else {
// Otherwise, start over at the top of the page and find first match.
if (n > 0) {
n = 0;
findInPage(str);
}
// Not found anywhere, give message. else
alert("Not found.");
}
}
return false;
}
有没有办法修改函数和/或使用不同的函数,以便它可以找到“加载”的 HTML 文档并在其中搜索输入的文本?
【问题讨论】:
-
您究竟是如何将文档加载到您的 div 中的?您使用 AJAX 或 iframe 还是 ...?
-
我不使用 iframe。相反,我将 的 div 中
标签: javascript html