-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyWords
More file actions
20 lines (18 loc) · 817 Bytes
/
keyWords
File metadata and controls
20 lines (18 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//查找页面keyword并跳到第一个keyword的位置
require("jquery",function(){
$("body").on("click","#find",function(){
var keyword = $("#key-words").val();
var $parent = $("p:contains('"+ keyword +"')").eq(0);
if($parent!==""){
var top = $parent.position().top;
$parent.html(function(i,h){
return h.replace(keyword,"<b style='color:red;'>"+keyword+"</b>");
});
$("body").scrollTop(top);
}
})
})
window.getSelection()获取鼠标选中文字
window.getSelection().focusNode.parentNode.tagName获取选中文字的外层标签
window.getSelection().focusNode.parentNode.setAttribute("target","_blank")给选中的文字父节点添加属性
window.getSelection().getRangeAt(0).surroundContents(document.createElement("span"))鼠标选中的文字包裹一层span