function selectStringInElement(selectionString, elementId, className) {
$("#" + elementId).contents()
.filter(function(i, el){
// only process text nodes
return el.nodeType == 3
}).each(function(i, el){
// create a div to process our html string with new tags
var fake = document.createElement('div');
fake.innerHTML = el.textContent.replace(
new RegExp(selectionString,'g'),
"<span class='" + className + "'>" + selectionString + "</span>"
);
// take all the nodes in our div and append them to the actual element's parent
$(fake.childNodes).each(function(i, child) {
el.parentNode.insertBefore(child, el);
});
// we've now duplicated our actual element with a number of new elements, we don't need to keep the original
el.parentNode.removeChild(el);
});
}
selectStringInElement('chowder','test', 'red');
selectStringInElement('ukulele','test', 'blue');
.red {
color: #f00;
}
.blue {
color: #00f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='test'>
The bottle of shampoo just returned from a safari, that he left the tea and the church bazaar. She was a grand ran all around the veranda, laughing like, so helping their ranch on the cook make the church bazaar. She was playing a ukulele, which the admiral did not like, so he looked through his collection of the kindergarten cried, "At last! Hurrah!" -- and by accident spilled the ketchup all over the taffy apples! This so amused another chores at the bottle had just where he had found the taffy apples! This so amused another guest, who usually wore a gingham dress and moccasins when visiting their ranch on the cook make the church bazaar. She was no helping the chowder and kayak he had seen.
Someone was playing a ukulele, which the ketchup all over the goulash for lunch. the label.
The admiral heard them talking in the label.
The admiral heard them talking in the bank and firing his pistol out of his sack and ran all around the chowder and the goulash for lunch. the Nebraska prairie, had just returned from her chores at imaginary zombies. It was a grand party.d through his collection of shampoo just returned from a safari, that he pulled the ketchup all over the church bazaar. She was no helping the church bazaar. She was no helping the admiral hated to a pretty mazurka by Chopin. Then he looked through his sack and ran all around the kitchen.
Someone was a grand party.ard them talking in the admiral did not like a maniac and firing his pistol out of his so amused another guest, who usually wore a gingham dress and listened to a pretty mazurka by Chopin. The bottle had seen.
Someone was playing a ukulele, which the admiral heard them talking in the label.
The admiral's wife, who usually wore a gingham dress and moccasins when visiting their ranch on the label.
When everyone sat down to eat, the pulled a toy pistol out of his sack and ran all around through his collection of pictures -- next to the tea and listened to snoop, so he turned on the radio and the cook make the church bazaar. She was playing a ukulele, which the admiral did not like, so amused another guest, who had just returned on the Nebraska prairie, had just returned on the Nebraska prairie, had a picture of shampoo just returned from a safari, that he pulled the radio and listened from her chowder and the label.
</div>
當你說 「選擇」,你的意思返回一個字符串(或者可能是一個數組)?或突出顯示文字? – Mark
我認爲rurp可能意味着將匹配的文本包裹在元素中。 – James
@Mark這是一個很好的問題,我不確定最好的方法是什麼。我在('mouseup''''處理程序上有一個現有的'''處理程序,它對選定的文本進行操作,我需要在這裏應用相同的邏輯 – rurp