开发者

Javascript -- filter set of links and style

Here is my set of links:

<ul id="power">
    <li><a id="10watt" name="power" href="#">10 watt</a></li>
    <li><a id="25watt" name="power" href="#">25 watt</a>
    <li><a id="30watt" name="power" href="#">30 watt</a>
    <li><a id="40watt" name="power" href="#">40 watt</a>
    <li><a id="50watt" name="power" href="#">50 watt</a>
    <li><a id="60watt" name="power" href="#">60 watt</a>
    <li><a id="75watt" name="power" href="#">75 watt</a>
    <li><a id="100watt" name="power" href="#">100 watt</a>
    <li><a id="120watt" name="power" href="#">120 watt</a>
    <li><a id="150watt" name="power" href="#">150 watt</a>
</ul>

and then I have an array:

var nine = ['25watt','30watt','40watt','50watt','60watt','75watt'];

I want to filter the links开发者_运维问答 so the ones not in the array are styled with the "unavailable" class defined in my CSS. Here is my jQuery.

if (this.id == '93') {
           $('a [name="power"]').filter(function() {
               if ($.inArray($(this).attr('id'), nine) < 0)
               $(this).addClass('unavailable');
               $(this).removeAttr('href');
           });
      }

this.id comes from the previous set of links, How do I write my filter statement, because what I have isn't working. Thanks for your help.


This should work:

$('a[name="power"]').filter(function() {
    return $.inArray($(this).attr('id'), nine) == -1;
}).addClass('unavailable').removeAttr('href');

Your selector had a space between a and [name="power"], which means that you were selecting all [name="power"] that are descendants of a, not a itself. Besides that, i corrected the filter function to do the filtering only.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜