我觉得最直接的方式做到这一点是通过使用智能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