我使用下面的代碼來替換div中的文本。但我需要循環通過特定的div,而不是頁面上的所有內容,即div的文本節點...通過傳遞div id,我可以更改文本替換的目標嗎?
我只是不明白如何引用下面的代碼中的div。
(function (parent) {
var childs = parent.childNodes;
// if there are children to this
if (childs && childs.length) {
// loop through each text node
for (var i = 0, node; node = childs[i]; i++) {
更多的代碼的請求:
function npup(parent) {
var children = parent.childNodes, child;
for (var idx=0, len=children.length; idx<len; ++idx) {
child = children.item(idx);
alert(child);
if (child.nodeType===3) {
// it is a text node. do magic.
for (var x = 0; x < en_count; x++) {
// what is the current content of the current node
var value = content(child);
// get current english phrase
var from = en_lang[x];
// get current other language phrase
var to = other_lang[x];
if (typeof(from) != 'undefined' && value.match(from)) {
content(node, value.replace(from, to));
}
}
}
}
} 變種theDiv =的document.getElementById( 'mydiv');
npup(theDiv);
我想更改上面的代碼,只能循環通過div的文本節點。它已經循環遍歷頁面的所有文本節點。 – crosenblum 2010-03-29 20:37:22
我想知道你的意思是文本節點。你是指3類型的DOM節點,還是其實是指其中包含文本的某些元素?請解釋你正在嘗試做什麼,你肯定會得到幫助。 在您最新的評論後,我改變了我的答案(下面),但我仍然不確定我是否完全瞭解你的內容。 – npup 2010-03-29 20:44:12