最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Look for multiple classes with jQuery - Stack Overflow

programmeradmin3浏览0评论

I have a simple script to hide/show a table row. I'd like it to look for an additional class, but I can seem to get it to work. Here is the original:

    jQuery(function () {
    jQuery(".toggle").click(function () {
        jQuery(this).closest("tr").next("tr").find(".hide1").slideToggle("none");
    });
});

I tried adding an 'or' operator to specify another class, but that doesn't work:

    jQuery(function () {
    jQuery(".toggle").click(function () {
        jQuery(this).closest("tr").next("tr").find(".hide1" || ".hide2").slideToggle("none");
    });
});

Can someone point out my error - Javascript is not my strong suit.

I have a simple script to hide/show a table row. I'd like it to look for an additional class, but I can seem to get it to work. Here is the original:

    jQuery(function () {
    jQuery(".toggle").click(function () {
        jQuery(this).closest("tr").next("tr").find(".hide1").slideToggle("none");
    });
});

I tried adding an 'or' operator to specify another class, but that doesn't work:

    jQuery(function () {
    jQuery(".toggle").click(function () {
        jQuery(this).closest("tr").next("tr").find(".hide1" || ".hide2").slideToggle("none");
    });
});

Can someone point out my error - Javascript is not my strong suit.

Share Improve this question asked Jan 20, 2012 at 19:35 PeachyPeachy 6534 gold badges21 silver badges31 bronze badges 3
  • Please post the HTML you're working with. – David Thomas Commented Jan 20, 2012 at 19:37
  • 1 I don't really think it is necessarily in this case. The JS seemed to be clear enough given the quantity of correct answers. Thanks everyone! – Peachy Commented Jan 20, 2012 at 19:48
  • Possibly not necessary, but it might have improved the answers at least a little to see what you're working with. But if the posted answers work, and solve your problem, then perhaps not in this case. – David Thomas Commented Jan 20, 2012 at 19:50
Add a ment  | 

5 Answers 5

Reset to default 9

Use a ma. This is the multiple selector.

.find(".hide1,.hide2")

Doing...

.find(".hide1" || ".hide2")

...is valid code, but it will be interpreted as...

.find(".hide1")

try this:

find(".hide1,.hide2")

See: Multiple Selector

just give it ma separated

.find(".hide1,.hide2").

To find either class-name, just use a ma-separated list:

.find('.hide1, .hide2')

This assumes, of course, that the rest of your jQuery works.

This should do the trick!

jQuery(function () {
    jQuery(".toggle").click(function () {
        jQuery(this).closest("tr").next("tr").find('.hide1, .hide2').slideToggle("none");
    });
});
发布评论

评论列表(0)

  1. 暂无评论