3

我想用“@@@@”之类的东西替换所有显示的文本。这意味着用户将看到所有页面都充满了“@@@@”而不是文本(除了图像、iframe 或页面的 HTML 代码中不存在的东西)。

这几乎取代了页面的html代码,但不影响标签和代码,只是显示给用户的文本。

例如,我想替换此页面中的所有文本:

<!DOCTYPE html>
<html>
<body>                  
<ul class="topnav">
    <li>Item 1</li>
    <li>Item 2 
        <ul><li>Nested item 1</li><li>Nested item 2</li><li>Nested item 3</li></ul>
       </li>
    <li>Item 3</li>
</ul>
<div>DIV1</div>
<div>DIV2</div>
<span>SPAN</span>
<table>
<tr>
    <td>Username</td>
</tr>
<tr>
    <td>Password</td>
</tr>
</table>
<p>
  <input type="checkbox" name="remember" tabindex=3 />
  <label for="checkbox">Remember <strong>password</strong></label>
</p>
<p>Click here to <a href='register.php'>Register</a></p>
</body>
</html>

结果应该是:

<!DOCTYPE html>
<html>
<body>                  
<ul class="topnav">
    <li>@@@@</li>
    <li>@@@@ 
        <ul><li>@@@@</li><li>@@@@</li><li>@@@@</li></ul>
       </li>
    <li>@@@@</li>
</ul>
<div>@@@@</div>
<div>@@@@</div>
<span>@@@@</span>
<table>
<tr>
    <td>@@@@</td>
</tr>
<tr>
    <td>@@@@</td>
</tr>
</table>
<p>
  <input type="checkbox" name="remember" tabindex=3 />
  <label for="checkbox">@@@@<strong>@@@@</strong></label>
</p>
<p>@@@@<a href='register.php'>@@@@</a></p>
</body>
</html>

我已经尝试过一些:

使用 JQuery 替换所有仅包含纯文本的元素、标签(及其子标签),这在开始时效果很好:

<ul class="topnav">
    <li>@@@@</li>
    <li>@@@@ 
        <ul><li>@@@@</li><li>@@@@</li><li>@@@@</li></ul>
       </li>
    <li>@@@@</li>
</ul>
<div>@@@@</div>
<div>@@@@</div>
<span>@@@@</span>

但最近我意识到,如果元素,标签有孩子,它会失败:

<p>
  <input type="checkbox" name="remember" tabindex=3 />
  <label for="checkbox">Remember <strong>password</strong></label>
</p>
<p>Click here to <a href='register.php'>Register</a></p>

所以我尝试了另一种方式,使用 document.body.innerText 选择所有文本,但是 HTML 格式丢失了。

我好累。有人能帮我吗?

非常感谢!

4

4 回答 4

9

这段代码似乎对我有用:

$('*').contents().filter(function() {
    return this.nodeType == Node.TEXT_NODE && this.nodeValue.trim() != '';
}).each(function() {
    this.nodeValue = '@@@@';
});

基本上,它将每个文本节点的内容替换为@@@@.

有关演示,请参见此处:http: //jsfiddle.net/K8544/3/

于 2012-06-23T06:20:27.880 回答
1

试试这个。这是一个用“ * ”替换内部文本的函数,但前提是其内部 HTML 等于其内部文本。否则,它会递归调用自身,向下导航 DOM,直到到达最里面的元素。

    $(document).ready(function() {
        function replaceChildren(node) {
            if ($(node).html() == $(node).text()) $(node).text("@@@@");
            else {
                $(node).children().each(function() {
                    replaceChildren(this);
                });
            }
        }
        replaceChildren($("body"));
    });

它并不完美,但对于大多数用途来说应该非常接近。我在 Stack Overflow 页面上尝试过,大多数文本都被替换了。唯一不起作用的地方是同一标签内有杂散标记和文本,例如<div>Here is some inner text <strong>and markup</strong></div>。也许这足以满足您的目的...

于 2012-06-23T06:20:59.557 回答
1

仅限 JQuery 的解决方案:

$("body *").contents().wrap("<obscure></obscure>");
$("obscure").each(function(i,e) {if ($(e).children().length==0) $(e).replaceWith("@@@@");}​);
$("obscure > *").unwrap();

http://jsfiddle.net/cranio/CW9jY/1/

此代码使用自定义标签 ( ) 包装每个节点obscure;的使用.contents()确保我们也包装纯文本节点。然后我们obscure用@@@@ 替换没有子节点的节点(之前是纯文本节点),因此也消除了obscure标签。最后,我们解开用 . 包裹的其他元素<obscure>

于 2012-06-23T06:29:54.227 回答
1

您可以匹配两个标签之间的任何内容 - 这些标签不必是相同的标签。例如<div>aaa<a href="bbb">ccc dd</a></div>,它会找到aaa,将其替换为@@@,然后通过查找 between和 next来查找ccc dd并替换它。@@@ @@><

<script type="text/javascript">
function obscure() {
    var newhtml = document.body.innerHTML.split("<");
    for (var i=1; i<newhtml.length; i++) {
        var list = newhtml[i].split(">");
        newhtml[i] = (list[0]) + ">" + ((list[1]).replace(/[^@\s]/gim, "@"));
    }
    newhtml[0] = (newhtml[0].replace(/[^@\s]/gim, "@"));
    document.body.innerHTML = newhtml.join("<");
}
</script>

(注意:这不会替换空格,因为这似乎会导致一些问题)。

于 2012-06-24T03:33:21.703 回答