开发者

jQuery and wordpress question.. Compare link title to text in certain div and if equal add a class to list item

I've had a programming problem that's driving me crazy for the last few days. I've searched through the wordpress doc and there's no way to do it via php but I know it can be done client side. In the sidebar of my application I have a list of all the post titles and a link to each.

.nav li a

I then have a div lets call it .random. This div has text in it... So what I'm trying to do is..

If the title of .nav li a is equal to

$('.random').text()

I'd like to add a class to that link.

Unfortunately I seem to be struggling with the ".each()" function in jQuery to get this to work.. Any help would be much apprecia开发者_StackOverflowted!..


Just

$(".nav li a[title="+$('.random').text()+"]").addClass('myclass');

?

But if $('.random').text() has character like ] it will be an issue.

Edit

To solve this issue you could either escape it, or use a filter :

$(".nav li a").filter(function(){
    return $(this).attr("title")==$('.random').text();
}).addClass('myclass');

Or a each :

$(".nav li a").each(function(){
    if($(this).attr("title")==$('.random').text()) $(this).addClass('myclass');
});


this may work, if i understand correctly

var r = $('.random').text();
$('a[title*="' + r + '"]').addClass('newclass');

edit to new value of $('.random').text()

edited again to change class if link title contains $('.random').text()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜