簡體   English   中英

搜索正文HTML中不包含html標簽的字符串

[英]Search a String excluding html tags in body HTML

如果我的HTML是這樣的

  <body>
  <b><span>jQuery</span> 
               is designed to change the way that you write 
  <span><i>JavaScript</i></span>.</b>
  </body>

我如何搜索字符串

  jQuery is designed to change the way that you write JavaScript // This is a Dynamic string

在正文html中,並用<span class="we">標記包裝。

請幫助我。

您正在尋找$(htmlString).text()

將此代碼放在頁面標題中(確保您還引用了jquery庫):

<script type="text/javascript">
    $(function() {
        $('b').wrap('<span class="we"/>');
    });
</script>

如果需要更具體,請將一個類放在您的b標簽上,使其看起來像這樣

<script type="text/javascript">
    $(function() {
        $('b.wrapme').wrap('<span class="we"/>');
    });
</script>

<body>
  <b class="wrapme"><span>jQuery</span> 
               is designed to change the way that you write 
  <span><i>JavaScript</i></span>.</b>
</body>

您可以通過獲取文本所在元素的innerHTML來實現。 在JavaScript中,它是:

document.body.innerHTML
$('body').contents().filter(function() {
    return $('span', this).length == 2 && $('i', this).length == 1;
}).wrap('<span class="we"></span>');

暫無
暫無

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

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