代码之家  ›  专栏  ›  技术社区  ›  Fez Vrasta

jQuery包含忽略HTML

  •  0
  • Fez Vrasta  · 技术社区  · 12 年前

    我有以下HTML代码:

    <pre class="">
        <span style="padding-right: 0.1px;">
            <span class="cm-operator">=</span>
            <span class="cm-operator interactive-linter-warning">===</span>
            <span class="cm-operator interactive-linter-warning interactive-linter-error">===</span>
        </span>
    </pre>
    

    它看起来、渲染像:

    =======
    

    我需要一种方法来选择 <pre/> 元素基于元素的内容进行选择 :contains :

    $("pre:contains('=======')")
    

    但它不起作用,可能是因为它甚至考虑了包含在 <前/> 要素

    我怎么能忽略HTML而只考虑文本呢?

    3 回复  |  直到 12 年前
        1
  •  1
  •   CodingIntrigue    12 年前

    我会的 .filter 按每个文本 pre 元素并修剪多余的空白:

    $("pre").filter(function() {
        return $(this).text().replace(/\s/g, "") === "=======";
    });
    

    jsFiddle

        2
  •  0
  •   Stphane    12 年前

    我会尝试:

    $("pre").filter(function(){ return $(this).text().indexOf('=======') != -1 });
    

    防止等号之间的空格

    $("pre").filter(function(){
          return $(this).text().replace(/=\s+/g,'=').indexOf('=======') != -1
      }
    );
    

    但仍然不轻

        3
  •  0
  •   Fez Vrasta    12 年前

    试试这个-

    $('pre').each(function(){
       var val = $.trim($(this).text().replace(/\n+\s+/g,''));
       if( val == '======='){
          alert($(this).attr('class'));    
       }
    });
    

    Demo

    推荐文章