jQuery 可以执行 if 语句吗?

发布于 2024-10-10 12:03:04 字数 503 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千紇 2024-10-17 12:03:04

jQuery 不是一种语言,它是 JavaScript 的一个库。答案是:“是的,JavaScript 有 if 语句作为语言的一部分。”

我不明白你写的伪逻辑。 “如果[某个列表项]'包含类'[foo] [bar]”是什么意思?如果列表项同时具有这两个类?那些课程中的任何一个?或者,如果列表项的后代至少具有这些类之一?或者,如果列表项有一个或多个具有所有这些类的后代?

这是使用一种解释的解决方案:

jQuery(function($){
  var li = $('#menu-main-menu li.menu-item-1061');
  if ( li.find('.current-menu-ancestor, .current-menu-parent').length ){
    // do whatever you want here.
  }
});

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:

jQuery(function($){
  var li = $('#menu-main-menu li.menu-item-1061');
  if ( li.find('.current-menu-ancestor, .current-menu-parent').length ){
    // do whatever you want here.
  }
});
独自←快乐 2024-10-17 12:03:04

只要对这个存在性做出适当的 if 声明就可以了吗?

$('#menu-main-menu li.menu-item-1061.current-menu-ancestor.current-menu-parent')

Just make a proper if statement on the existence of this?

$('#menu-main-menu li.menu-item-1061.current-menu-ancestor.current-menu-parent')
杀手六號 2024-10-17 12:03:04

Javascript 可以执行 if 语句,但在 jQuery 中你可以使用 is 函数
确定任何 jquery 对象是否适合另一个表达式。

例如,您有这个 jquery 对象

var firstObj = $(".selector1")

,并且想要测试是否有任何元素适合此表达式

".selector2"

,那么您可以使用 is ,如下所示

firstObj.is(".selector2")

如果有任何元素适合,则响应将为“true”,否则“错误的'。
所以你可以结合javascript的if如下

if (firstObj.is(".selector2")){
    // you can do something here
}

希望它有帮助

Javascript can perform if statement, but in jQuery you can use is function
to determine if any jquery object fit another expression.

for example, you have this jquery object

var firstObj = $(".selector1")

and you want to test if any of the element fit this expression

".selector2"

then you can use is as follows

firstObj.is(".selector2")

If any element fits then the response will be 'true', otherwise 'false'.
so you can combine with javascript's if as follows

if (firstObj.is(".selector2")){
    // you can do something here
}

hope it helps

简单气质女生网名 2024-10-17 12:03:04

正如所指出的,if 是 JavaScript 的构造,而不是特别的 jQuery。我认为您正在寻找的是 jQuery 函数 hasClass,它可以根据类名检查 DOM 元素,如果元素当前具有该类,则返回 true。

您可以这样使用它:

if (myElement.hasClass("myClass")) {
    // do something
}

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 function hasClass, 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:

if (myElement.hasClass("myClass")) {
    // do something
}

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 and current-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 class menu-item-1061 sounds like it should be an ID (it's unique), not a class. Either way, the hasClass function will still work for you, since it returns true if any of the matched elements have the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文