1. 程式人生 > >js-檢索文字高亮顯示

js-檢索文字高亮顯示

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.1.min.js"></script>
<script>
$(function(){
$("#codearea").bind("click",function(){getSelect($("#codearea"))});
$("#codearea").children().each(function(){
$(this).bind("click",function(){getSelect(this)});
})
})
 function getSelect(obj)
    {
        if(window.getSelection)
        {
var count=0;
var code=window.getSelection()+"";
           if(undefined!=code&&0!=code.length&&""!=code){
var codehtml=$(obj).text();
var newhtml=""
for(var i=0;i<codehtml.length;i++){
var newcode=codehtml.substring(i,i+code.length);
if(code==newcode){
newhtml+="<span style='color:red'>"+newcode+"</span>";
i=i+code.length;
count++;
}else{
newhtml=newhtml+codehtml.substring(i,i+1);
}
}
$(obj).text("");
$(obj).html(newhtml);
alert("共檢索到"+count+"條")
}
        }
        else
        {
alert("====")
            alert(document.selection.createRange().text);
        }
        
    }
</script>
<p>js-滑鼠左擊選中檔案檢索全文</p>
<div id="codearea">
<div>
List 111111111111111111 logo
</div>
@Test public void shouldDecapitalizeSomeCharsUntilItFindsOneUppercased() throws NoSuchMethodException {
  Assert.assertEquals("urlClassLoader",extractor.nameFor(URLClassLoader.class));
  Assert.assertEquals("bigDecimal",extractor.nameFor(BigDecimal.class));
  Assert.assertEquals("string",extractor.nameFor(String.class));
  Assert.assertEquals("aClass",extractor.nameFor(AClass.class));
  Assert.assertEquals("url",extractor.nameFor(URL.class));
}
</div>