搜索body html中不包含html标记的字符串

Search a String excluding html tags in body HTML

本文关键字:html 字符串 body 搜索 包含      更新时间:2023-09-26

如果我的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

在body 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>');