2

我想在文本中搜索并在查找行上滚动。我无法开发此代码以滚动到找到的<h3>标记行。

jQuery:

$('#searchfor').keyup(function(){
         var page = $('#all_text');
         var pageText = page.text().replace("<span>","").replace("</span>");
         var searchedText = $.trim($('#searchfor').val());
         var theRegEx = new RegExp("("+searchedText+")", "igm");    
         var newHtml = pageText.replace(theRegEx ,"<span>$1</span>");
         page.html(newHtml);
    });

HTML:

<input type="text" id="searchfor"/>
<p id="all_text">
<h3>LINE 1</h3>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euism modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tinci futurum.<b>EX1</b><br/>
<h3>LINE 2</h3>        
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euism modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tinci futurum.<b>EX2</b><br/>
.
.
.
<h3>LINE 100</h3>        
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euism modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tinci futurum.<b>EX100</b><br/>
</p>
4

1 回答 1

-1

是否要滚动到刚刚添加的跨度<span>$1</span>

 $("span").scrollintoview();

请注意,如果跨度已经在可见区域中,它将不会滚动。

更新之前 错过了提及插件。这是插件页面的链接

于 2012-12-31T20:51:54.533 回答