簡體   English   中英

根據文本獲取 div 元素

[英]Get div element based on text

我有下面的代碼。 我想檢索包含以下文本的元素。 我不知道文本在哪個 div 中,所以必須搜索整個頁面。

$('*', 'body')
        .addBack()
        .contents()
        .filter(function(){
            return this.nodeType === 3;
        })
        .filter(function(){
            // Text below: {launch=[MODULE]}
            return this.nodeValue.indexOf('{launch=[MODULE]}') != -1;
        })
        .each(function(){
          // How do I receive the element here?
          console.log(this);
        });

提前致謝!

在 javascript 中,您可以收集所有<div>元素,然后遍歷它們,每次檢查textContent屬性。

例子:

// GRAB ALL THE DIVS
const allDivs = [...document.getElementsByTagName('div')];

// SET THE SEARCH PHRASE
const searchPhrase = '{launch=[MODULE]}';

// LOOP THROUGH ALL THE DIVS, LOOKING FOR THE SEARCH PHRASE
for (let div of allDivs) {

  if (div.textContent !== searchPhrase) continue;

  console.log(div);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM