jQuery 确定 ul 是否有类或另一个类 [英] jQuery determine if ul has class OR another one

查看:22
本文介绍了jQuery 确定 ul 是否有类或另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定一个对象是有一个类还是另一个类的正确方法是什么?以下似乎是错误的..

if ($('#menu-item-49').hasClass('current-menu-item' || 'current-menu-parent') ) {$('ul.sub-menu').css('display', 'block');}

谢谢!

解决方案

你可以使用 is?

if ($('#menu-item-49').is('.current-menu-item, .current-menu-parent')) {$('ul.sub-menu').css('display', 'block');}

<块引用>

检查当前匹配的集合针对选择器的元素并返回如果这些元素中至少有一个,则为真匹配选择器.

击败必须使用多个 hasClass 查询,这是替代方案:

if ($('#menu-item-49').hasClass('current-menu-item') ||$('#menu-item-49').hasClass('current-menu-parent')) {$('ul.sub-menu').css('display', 'block');}

what is the right way to determine if an object has one class OR another one? The following is appearantly wrong..

if ($('#menu-item-49').hasClass('current-menu-item' || 'current-menu-parent') ) {
  $('ul.sub-menu ').css('display', 'block');
}

Thanks!

解决方案

You could use is instead?

if ($('#menu-item-49').is('.current-menu-item, .current-menu-parent')) {
  $('ul.sub-menu ').css('display', 'block');
}

Check the current matched set of elements against a selector and return true if at least one of these elements matches the selector.

Beats having to use multiple hasClass queries, which is the alternative:

if ($('#menu-item-49').hasClass('current-menu-item') || 
    $('#menu-item-49').hasClass('current-menu-parent')) {
  $('ul.sub-menu ').css('display', 'block');
}

这篇关于jQuery 确定 ul 是否有类或另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆