これは、言語翻訳 jquery スクリプトの多くの部分の 1 つとして使用しています。
この部分は、Web ページ上のすべてのノードをループ処理するときに、ノードのテキストを取得します。
ただし、多くの非表示の JavaScript もテキスト ノードとして取得します。
HTML側だけを取得するために、これを変更する方法はありますか?さらに、不要な空白を削除しますか?
これが元のコードです。
var content = function (node, txt) {
if (txt) {
if (node.textContent) {
node.textContent = txt;
} else if (node.nodeValue) {
node.nodeValue = txt;
}
} else {
return node.textContent ? node.textContent : node.nodeValue;
}
};
ここでは、このコードのコンテキストを示すのに役立ちます。
// recursive tree walker
(function (parent) {
var childs = parent.childNodes;
// if childs object has data
if (childs && childs.length) {
var i = childs.length; while (i--) {
// assign node variable to childs object
node = childs[i];
// text node found, do the replacement
if (node.nodeType == 3) {
// assign the current value to a variable
var value = content(node);
} else {
arguments.callee(node);
}
}
}
})(document.body);
これはすべて、私の言語翻訳コードが機能するロジックです。入力を微調整して、テキストを取得し、ページのソースにある JavaScript コードを取得しないようにしたいだけです。