我覺得最直接的方式做到這一點是通過使用智能jQuery highlight插件我碰到了。應用之後,你就可以做你以後的事情。下面是一個例子,並在最後一個鏈接到現場小提琴:
HTML
<p>Some examples of how to highlight words with the jQuery Highlight plugin. First an example to demonstrate the default behavior and then others to demonstrate how to highlight the words Facebook and Twitter with their own class names. Words will be highlighted anywhere in the DOM, one needs only to be specific on where the script should look for the words. It supports case-sensitivity and other options, like in the case of YouTube.</p>
CSS
p { line-height: 30px; }
span { padding: 4px; }
.highlight { background-color: yellow; }
.facebook { background-color: #3c528f; color: white; }
.twitter { background-color: #1e9ae9; color: white; }
.youtube { background-color: #be0017; color: white; }
高亮插件(需要後裝載jQuery
和之前的JavaScript
)
<script type="text/javascript" src="http://github.com/bartaz/sandbox.js/raw/master/jquery.highlight.js"></script>
JS
// default
$("body p").highlight("default");
// specify class name (not case sensitive)
$("body p").highlight("twitter", { className: 'twitter' });
$("body p").highlight("facebook", { className: 'facebook' });
// specify class name (case sensitive)
$("body p").highlight("YouTube", { className: 'youtube', caseSensitive: true });
包含此JavaScript
頁面(在body
結束標記,這樣你就不需要使用下面的函數之前的底部:
$(document).ready(function() {
// unnecessary if you load all your scripts at the bottom of your page
});
Fiddle for the win! :)
使用JS來實現這一點。 –
從長遠來看,爲了給這些類加上前綴,你會更好。例如,「嘰嘰喳喳」而不是「嘰嘰喳喳」。否則,您可能在將來的造型上遇到問題。 –
@JamesDonnelly抱歉,我不太熟練的編碼網站,我更多地參與設計方面。你能以更簡單的方式解釋這個嗎?你將如何改變我在做什麼? – jordanhuxley