开发者

add class for equivalent other ID

<select id="list" multiple=multiple>
    <option value="1" id="one" selected="selected">one </option>
    <option value="2" id="two" selected="selected">two </option>
    <option value="3" id="three">three </option>
</select>

<ul id="uli">
    <li aaa="one">one</li>
    <li aaa="two">two</li>
    <li aaa="three">three</li>
</ul>

With the CSS:

.back {
 color: red;
}

And the Javascript:

$("#list").find("option:selected").each(function() {
    // logic here
});

Into the each() callback, I want to add class for equivalent in uli. I would like to use addClass(.back) for li where attr aaa == id for list.

live: http://jsfiddle.n开发者_高级运维et/yjL9n/3/


Here you go:

$("#uli").find('[aaa="'+this.id+'"]')
    .addClass('back');

http://jsfiddle.net/yjL9n/4/


This works (and is tidier):

$("#list option:selected").each(function() { 
    $('#uli li[aaa="' + this.id + '"]').addClass('back');
});

http://jsfiddle.net/yjL9n/5/

However I wouldn't use aaa as an attribute!


$('li[aaa=' + this.id + ']').addClass('back');

$('table').find('td[aaa="'+this.id+'"]').addClass('back');

added to the inner part of your fiddle will do it. Updated based on comment from OP.


Try this

$("#list").find("option:selected").each(function() {
   $("#uli").find('[aaa="'+this.id+'"]').addClass('back');
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜