0

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

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;
});​
4

1 回答 1

0

你应该定义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>
于 2012-06-04T13:04:16.640 回答