简体   繁体   English

替换功能在ie8 ie7中不起作用

[英]Replace function doesn't work in ie8 ie7

My simple text search works well in IE9 but not in IE8 & IE7. 我的简单文本搜索在IE9中效果很好,但在IE8和IE7中效果不佳。 There must be something wrong with the replace function. 替换功能一定有问题。 Could you please help? 能否请你帮忙? http://jsfiddle.net/8zuCP/ http://jsfiddle.net/8zuCP/

  <input type="text" id="searchfor"/>   
  <span class="search">"Wrecking Ball"</span>   
  <span class="search">We clawed, we chained our hearts in vain</span> 
  <span class="search">We jumped never asking why</span> 
  <span class="search">We kissed, I fell under your spell.</span> 
  <span class="search">A love no one could deny</span> 


  <script>
  $('#searchfor').keyup(function(){
  $('.search').each(function() {
        var line = $(this);
        var lineText = line.text().replace("<hl>","").replace("</hl>");
        var searchedText = $('#searchfor').val();
        var regText = new RegExp("("+searchedText+")", "igm");  
        var newHtml = lineText.replace(regText,"<hl>$1</hl>");
        line.html(newHtml);
  });
  });
  </script>

CSS: CSS:

    .search hl
    {
            background-color:orange;
    }

It's choking because <hl> (which, at first, I thought was a number 1, and not a lowercase "L") is not a valid HTML tag and the earlier versions of IE don't like that. 之所以令人窒息,是因为<hl> (起初我以为是1,而不是小写的“ L”)不是有效的HTML标记,而IE的早期版本不喜欢这种标记。

I'd recommend turning your "hl" into a class and assigning it to a <span> tag in place of your custom <hl> tag. 我建议将“ hl”变成一个类,并将其分配给<span>标记,以代替自定义的<hl>标记。 Your HTML is fine . 您的HTML很好。 . . you just need to change your JS and CSS: 您只需要更改您的JS和CSS:

JS JS

$('#searchfor').keyup(function(){
    $('.search').each(function() {
        var line = $(this);
        var lineText = line.text().replace(/<\/?span[^>]*>/, "");
        var searchedText = $('#searchfor').val();
        var regText = new RegExp("("+searchedText+")", "igm");
        var newHtml = lineText.replace(regText,"<span class='hl'>$1</span>");
        line.html(newHtml);
    });
});

CSS CSS

.search span.hl {background-color: orange;}

Alternately, if you will never have any other <span> tags in your "search" spans, you could just add the plain <span> , with no class, like this: 或者,如果您在“搜索”范围中永远没有任何其他<span>标记,则可以只添加普通的<span> ,而不添加类,如下所示:

        var lineText = line.text().replace(/<\/?span>/, "");
            . . .
        var newHtml = lineText.replace(regText,"<span>$1</span>");

. . . and change the style to: 并将样式更改为:

.search span {background-color: orange;}

A couple of side notes: 一些注意事项:

  1. I also updated your initial .replace() to remove the existing tags . 我还更新了您的初始.replace()来删除现有标签。 . . using regex, you can do it in one call, instead of two. 使用正则表达式,您可以一次调用,而不是两次调用。
  2. The initial .replace("</hl>") was missing the replacement text . 最初的.replace("</hl>")缺少替换文本。 . . it needed to be .replace("</hl>", "") . 它必须是.replace("</hl>", "") That actually made it fail in IE9, when I tested it (thought Firefox took it like a champ :) ) 实际上,这使它在IE9中失败,当我对其进行测试时(Firefox认为它像冠军:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM