2

Web ページの本文領域だけがアクセス可能な部分である場合、インライン JavaScript または別のインライン対応言語を使用して、特定のテキスト フレーズ (HTML で記述) のすべてのインスタンスを削除する方法はありますか?

これは、Tiny.cc/customurl を使用していて、「tiny.cc/」を示す部分を削除したい場合など、多くの状況で役立ちます。


詳細が許可されている場合は、Tiny.cc を使用してカレンダー プラグインを変更し、カスタム URL (tiny.cc/customurl) を作成します。プラグインはデフォルトで完全な URL を表示するので、「tiny.cc/」というテキストを取り除き、コードの「customurl」部分を保持します。

<div class="ews_cal_grid_custom_item_3">
  <div class="ews_cal_grid_select_checkbox_clear" id="wGridTagChk" onclick="__doPostBack('wGridTagChk', 'tiny.cc/Baseball-JV');" >&nbsp;</div>
                            tiny.cc/Baseball-JV
  </div>

削除したい部分はhttp://tiny.cc/3 行目だけです。

4

3 に答える 3

1

すべての HTML を置き換えずに (すべてのイベント ハンドラーを破壊する)、再帰を行わずに (通常はより高速に) これを行うには、次のようにします。

function removeText(top, txt) {
    var node = top.firstChild, index;
    while(node && node != top) {
        // if text node, check for our text
        if (node.nodeType == 3) {
            // without using regular expressions (to avoid escaping regex chars),
            // replace all copies of this text in this text node
            while ((index = node.nodeValue.indexOf(txt)) != -1) {
                node.nodeValue = node.nodeValue.substr(0, index) + node.nodeValue.substr(index + txt.length);
            }
        }
        if (node.firstChild) {
            // if it has a child node, traverse down into children
            node = node.firstChild;
        } else if (node.nextSibling) {
            // if it has a sibling, go to the next sibling
            node = node.nextSibling;
        } else {
            // go up the parent chain until we find a parent that has a nextSibling
            // so we can keep going
            while ((node = node.parentNode) != top) {
                if (node.nextSibling) {
                    node = node.nextSibling;
                    break;
                }
            }
        }
    }
}​

ここでの動作デモ: http://jsfiddle.net/jfriend00/2y9eH/

ドキュメント全体でこれを行うには、次のように呼び出します。

removeText(document.body, "http://tiny.cc/Baseball-JV");
于 2012-08-22T17:09:20.187 に答える
0

HTML内のその文字列のすべてのインスタンスを置き換えたくない場合は、ノード構造を再帰的に繰り返す必要があります。たとえば、次のようになります。

function textFilter(element, search, replacement) {
    for (var i = 0; i < element.childNodes.length; i++) {
        var child = element.childNodes[i];
        var nodeType = child.nodeType;
        if (nodeType == 1) { // element
            textFilter(child, search, replacement);
        } else if (nodeType == 3) { // text node
            child.nodeValue = child.nodeValue.replace(search, replacement);
        }
    }
}

次に、適切な要素をつかんで、この関数を呼び出します。

var el = document.getElementById('target');
textFilter(el, /http:\/\/tiny.cc\//g, "");​  // You could use a regex
textFilter(el, "Baseball", "Basketball");​   //  or just a simple string
于 2012-08-22T16:59:50.877 に答える
0

データを文字列形式で提供できる限り、正規表現を使用してこれを行うことができます。

body タグの innerHTML全体にアクセスできる場合は、それを解析できます。これは遅くてちょっと悪い方法ですが、説明のために:

document.body.innerHTML = document.body.innerHTML.replace(
    /http:\/\/tiny\.cc\//i,    // The regular expression to search for
    "");                       // Waht to replace with (nothing).

式全体がスラッシュで囲まれているため、正規表現内のスラッシュはバックスラッシュでエスケープする必要があります。

これは、ピリオドなど、正規表現で特別な意味を持つ他の文字にも当てはまります。単一のピリオド ( .) は、一致する「任意の」文字を示します。ピリオドに一致させるには、エスケープする必要があります ( \.)

編集:

onclick で URL への参照を保持したい場合は、正規表現を変更して、一重引用符で囲まれたときに一致しないようにすることができます (例として)。

/([^']http:\/\/tiny\.cc\/[^'])/i
于 2012-08-22T16:47:44.807 に答える