gpt4 book ai didi

javascript - 使用 jQuery 替换字母

转载 作者:行者123 更新时间:2023-11-28 03:26:32 25 4
gpt4 key购买 nike

我正在制作一个书签,可以将所有字母(例如“R”)更改为其他字母(例如“W”),或者可能替换整个单词。我尝试使用以下代码,但它弄乱了网站,并显示了元素中的元素。

javascript: $("body").text($("body").text().replace("r", "w"));

有没有办法解决这个问题?

对于那些想知道我会用它做什么的人,我正在尝试编写一个代码来改变它:

Hello, my name is Jonathan Grasswell, and I would like to propose a new idea for a tractor.

进入此:

Hewwo, my nyame is Jonnyathyan Gwyassweww, aynd I wouwd wike to pwopose a nyew ideya fow ay twactow.

基本上,是一个“OWofier”。另外,是否可以在一段时间后插入 20% 的随机表情符号?

最佳答案

BODY 如果充满了 HTML 内容。您的问题就在这里 $("body").text($,因为在 .text( 中您说的是:“使正文成为此文本:“丢失整个标记。
您应该迭代 3 (Node.TEXT_NODE) 的每个 NodeType,或者使用 TreeWalker API

const body = document.body; // Or whatever more specific element you desire
const repl = { // Your replacements:
"l" : "w",
"L" : "W",
"r" : "w",
"R" : "W",
"\\." : ". :)" // Remember, escape RegExp special characters to treat them as literals
};

const treeWalker = document.createTreeWalker(body, NodeFilter.SHOW_TEXT);

while (treeWalker.nextNode()) {
const Node = treeWalker.currentNode; // PS: Node.nodeType is always 3
Object.keys(repl).forEach(key => {
Node.nodeValue = Node.nodeValue.replace(new RegExp(key, "g"), repl[key]);
});
}
.hello, .ll {color: BURLYWOOD;} /* "l"s of "class" and "hello" will stay intact */
<h1 class="hello">Hello,</h1>
<p>
my name is <b class="ll">Long Island</b>,
and I would like to propose a new idea. For a clock. LOL
</p>

关于javascript - 使用 jQuery 替换字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58600082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com