jQuery 可以执行 if 语句吗?
jQuery 可以执行“if”函数吗?例如,如果一个菜单项中存在一个类,是否将该类应用于另一个菜单项?这与通常的活动页面菜单突出显示略有不同,因为我正在处理活动下拉菜单和子页面。
我想做的是
(the usual suspect) $(document).ready(function() {
如果...
$('#menu-main-menu li.menu-item-1061') "contains the classes
current-menu-ancestor current-menu-parent"
then add those same classes to, i.e.:
$('#menu-main-menu li.menu-item-1099').addClass('current-menu-ancestor
current-menu-parent')
}); });
应该是一种可行的方法...
Can jQuery perform an "if" function? As in, if a class exists in one menu item, apply that class to a different menu item? This is slightly different than the usual active page menu highlighting, as I'm dealing with active dropdown menus and child pages.
What I'd like to do is
(the usual suspect) $(document).ready(function() {
If...
$('#menu-main-menu li.menu-item-1061') "contains the classes
current-menu-ancestor current-menu-parent"
then add those same classes to, i.e.:
$('#menu-main-menu li.menu-item-1099').addClass('current-menu-ancestor
current-menu-parent')
}); });
Should be a way for this to work....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jQuery 不是一种语言,它是 JavaScript 的一个库。答案是:“是的,JavaScript 有
if
语句作为语言的一部分。”我不明白你写的伪逻辑。 “如果[某个列表项]'包含类'[foo] [bar]”是什么意思?如果列表项同时具有这两个类?那些课程中的任何一个?或者,如果列表项的后代至少具有这些类之一?或者,如果列表项有一个或多个具有所有这些类的后代?
这是使用一种解释的解决方案:
jQuery is not a language, it is a library for JavaScript. The answer is: "Yes, JavaScript has
if
statements as part of the language."I don't understand the pseudo-logic you have written. What do you mean by "If [some list item] 'contains the classes' [foo] [bar]"? If the list item has both of those classes? Either of those classes? Or if the list item has a descendant with at least one of those classes? Or if the list item has one or more descendants that have all of those classes?
Here is a solution using one interpretation:
只要对这个存在性做出适当的
if
声明就可以了吗?Just make a proper
if
statement on the existence of this?Javascript 可以执行
if
语句,但在 jQuery 中你可以使用is
函数确定任何 jquery 对象是否适合另一个表达式。
例如,您有这个 jquery 对象
,并且想要测试是否有任何元素适合此表达式
,那么您可以使用
is
,如下所示如果有任何元素适合,则响应将为“true”,否则“错误的'。
所以你可以结合javascript的
if
如下希望它有帮助
Javascript can perform
if
statement, but in jQuery you can useis
functionto determine if any jquery object fit another expression.
for example, you have this jquery object
and you want to test if any of the element fit this expression
then you can use
is
as followsIf any element fits then the response will be 'true', otherwise 'false'.
so you can combine with javascript's
if
as followshope it helps
正如所指出的,
if
是 JavaScript 的构造,而不是特别的 jQuery。我认为您正在寻找的是 jQuery 函数hasClass
,它可以根据类名检查 DOM 元素,如果元素当前具有该类,则返回 true。您可以这样使用它:
在
if
构造中,您可以插入要将类添加到其他元素的代码。更新:您的示例有点奇怪,因为您似乎想问“与此选择器匹配的一组元素是否具有类
current-menu-ancestor
和当前菜单父
?”这很奇怪,因为您正在匹配一组元素(您最具体的选择器是一个类,因此可能返回多个项目)。看来您的 HTML/CSS 中可能存在设计错误,因为menu-item-1061
类听起来应该是一个 ID(它是唯一的),而不是一个类。无论哪种方式,hasClass
函数仍然可以为您工作,因为如果任何 匹配元素具有该类,它就会返回true
。As pointed out,
if
is a construct of JavaScript, not jQuery in particular. I think what you're looking for is the jQuery functionhasClass
, which can check DOM elements against the name of a class and will return true if the element currently has that class.You can use it like this:
Inside the
if
construct, you can insert the code you want to add the classes to the other elements.Updated: Your example is a bit strange, in that you seem to wish to ask "does a set of elements matched against this selector have the classes
current-menu-ancestor
andcurrent-menu-parent
?" This is odd because you are matching a set of elements (your most specific selector is a class, so might return more than one item). It seems that you might have a design error in your HTML/CSS, since the classmenu-item-1061
sounds like it should be an ID (it's unique), not a class. Either way, thehasClass
function will still work for you, since it returnstrue
if any of the matched elements have the class.