无法弄清楚如何修复单词替换javascript

Can't figure out how to fix a word replace javascript

本文关键字:单词 替换 javascript 何修复 弄清楚      更新时间:2023-09-26

我想使用以下脚本更改页面上的一些单词。我不确定如何解决它...有人可以帮忙吗?

http://jsfiddle.net/cip691/yUMS7/

var dict = {
    "not": " NOT ",
    "is not": " IS NOT ",
    "like": " likeeee ",
    "Like": "lllike",
    "job": "JOB"
},
    terms = [],
    term;
for (term in dict) { terms.push(term);
var search = new RegExp('''b(' + terms.join('|') + ')''b', 'g');
                };
// now for every text node:
textNode.data = textNode.data.replace(search, function(full, match) {
    return dict[match] || match;
});​

你应该定义textNode

就像在这个例子中

链接示例

<head>
    <script type="text/javascript">
        function GetTextNode () {
            var textContainer = document.getElementById ("textContainer");
            var textNode = textContainer.firstChild;
            alert (textNode.data);
        }
    </script> 
</head>
<body>
    <div id="textContainer">This is a simple text in the container.</div>
    <button onclick="GetTextNode ()">Get the contents of the container</button>
</body>