[英]Change Link Color in Jquery UL li Filter
I am new to jquery. 我是jquery新手。 What I want to create is a Jquery Filter in Unordered list with links and input field on the top.
我要创建的是一个无序列表中的Jquery过滤器,其链接和输入字段位于顶部。 As I type letters in the input field I want the letters of links inside the li items to change color.
当我在输入字段中键入字母时,我希望li项目中的链接字母可以更改颜色。 Without link it is achievable but I need it for links(a href).
没有链接,这是可以实现的,但我需要它来链接(a href)。 for eg: If I type 'Mum' of Mumbai then the letters in the list having MUM should change to red color.
例如:如果我键入孟买的“妈妈”,则列表中具有妈妈的字母应更改为红色。 Have a look at the Jsfiddle
看看Jsfiddle
HTML 的HTML
<div id="location-search">
<p id="header">Select Your Prefered Location</p>
<ul id="list">
<li><a href="#">Mumbai</a></li>
<li><a href="#">Bangalore</a></li>
<li><a href="#">Goa</a></li>
<li><a href="#">Navi Mumbai</a></li>
<li><a href="#">Pune</a></li>
<li><a href="#">Thane</a></li>
</ul>
</div>
CSS 的CSS
body {background:#f3f3f3;}
#location-search {float:left; display:inline; width:280px; background:#fff; padding:20px;}
p#header {font:bold 15px Arial;}
input {
border:1px solid #ccc;
font-size:1em;
height:2.10em;
*height:1.0em;
line-height:1.0em;
padding:0.10em 0;
width:100%;
}
.filterform {
width:220px;
font-size:12px;
display:block;
}
ul#list {float:left; display:inline; width:224px; padding:10px 0;}
ul#list li {font:bold 13px Arial; padding:5px 0 5px 10px;}
ul#list li a {color:#646464;}
Javascript Java脚本
(function ($) {
jQuery.expr[':'].Contains = function(a,i,m){
return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
};
function listFilter(header, list) {
var form = $("<form>").attr({"class":"filterform","action":"#"}),
input = $("<input>").attr({"class":"filterinput","type":"text"});
$(form).append(input).appendTo(header);
$(input)
.change( function () {
var filter = $(this).val();
if(filter) {
$(list).find("a:not(:Contains(" + filter + "))").parent().slideUp();
$(list).find("a:Contains(" + filter + ")").parent().slideDown();
} else {
$(list).find("li").slideDown();
}
return false;
})
.keyup( function () {
$(this).change();
});
}
$(function () {
listFilter($("#header"), $("#list"));
});
}(jQuery));
And here's what I want to achieve: Screenshot 这是我想要实现的目标: 屏幕截图
Any help would be appreciated I am just stuck at this step. 任何帮助将不胜感激,我只是停留在这一步。 N all other stuff I have done it.
我没有其他所有事情。
Here is the working Demo http://jsfiddle.net/MF4Tn/12/ 这是工作中的演示http://jsfiddle.net/MF4Tn/12/
I have added two javascript functions: jQuery.fn.removeHighlight
and jQuery.fn.highlight
and little css code. 我添加了两个JavaScript函数:
jQuery.fn.removeHighlight
和jQuery.fn.highlight
和少量CSS代码。 javascript I took from the link that I posted below. 我从下面发布的链接中提取了javascript。
Here is written useful code example thanks to @dzordz 感谢@dzordz,这里是有用的代码示例
Here is another solution Demo http://jsfiddle.net/MF4Tn/28/ 这是另一个解决方案演示http://jsfiddle.net/MF4Tn/28/
I have made some in-line changes using substring
and then span
is added to change color 我使用
substring
进行了一些在线更改,然后添加了span
以更改颜色
i created a fiddle that may suit your needs: http://jsfiddle.net/MF4Tn/29/ 我创建了一个适合您需要的小提琴: http : //jsfiddle.net/MF4Tn/29/
Basicly it does: 基本上可以做到:
$(list).find("li>a").each(function(id,el){
var a=$(el);
if(a.text().toLowerCase().indexOf(filter.toLowerCase()) >= 0){
a.html(a.text().split(filter).join("<span style='color:red;'>"+filter+"</span>" ));
}else{
a.html(a.text());
}
});
This is how it is done: 这是这样做的:
I removed all the slideDown
and slideUp
things in order to have this example clean. 我删除了所有
slideDown
和slideUp
内容,以使此示例更干净。
One may even change the .split()
and .join()
thing into some string replace funktion. 甚至可以将
.split()
和.join()
更改为某种字符串替换功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.