【发布时间】:2013-06-04 06:58:21
【问题描述】:
我在示例页面上突出显示了插件。它的工作原理是当你输入一个单词时它会被突出显示,你可以用空格分隔它并插入另一个被突出显示的单词..
我的新问题,对我来说似乎尚未解决,例如我想要两个输入。第一个单词用一种颜色突出显示,第二个输入的单词用另一种颜色着色..
到目前为止我的情况是:http://jsfiddle.net/cfYrt/4/
html:
<body>
<input type="text" class="span1 disabled" id="field1" name="field1"><br>
<p>
Vestibulum rhoncus urna sed urna euismod, ut cursus eros molestie.
Nulla sed ante ut diam gravida auctor eu quis augue.
Donec eget diam malesuada, consectetur orci at, ultrices tellus.
Duis id dui vel sem consequat rutrum eget non orci.
Nullam sit amet libero odio. Vestibulum sapien sapien,
molestie quis porta nec, sodales nec felis.
Mauris vehicula, erat eu consectetur viverra,
dui tellus laoreet dolor, quis faucibus turpis eros non mi.
</p>
</body>
脚本:
$(function() {
$('#field1').bind('keyup change', function(ev) {
// pull in the new value
var searchTerm = $(this).val();
// remove any old highlighted terms
$('body').removeHighlight();
// disable highlighting if empty
if ( searchTerm ) {
var terms = searchTerm.split(/\W+/);
$.each(terms, function(_, term){
// highlight the new term
term = term.trim();
if(term != "")
$('body').highlight(term);
});
}
});
});
css
.highlight {
background-color: #fff34d;
-moz-border-radius: 5px; /* FF1+ */
-webkit-border-radius: 5px; /* Saf3-4 */
border-radius: 5px; /* Opera 10.5, IE 9, Saf5, Chrome */
-moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7); /* FF3.5+ */
-webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7); /* Saf3.0+, Chrome */
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7); /* Opera 10.5+, IE 9.0 */
}
.highlight {
padding:1px 4px;
margin:0 -4px;
}
字段 2 中输入的相应值是否可以在文本中以不同于字段 1 的颜色着色?这个插件是否可以使用它的核心代码调用class .highlight?
【问题讨论】:
标签: jquery input colors highlight