使用jquery确定是否存在子菜单

使用jquery确定是否存在子菜单,jquery,html,css,Jquery,Html,Css,我有一个html文件中的动态树菜单。结构是- 这是一个垂直的树菜单。菜单没问题。但我想显示一个小箭头,如果导航有子导航。像第三个。我想在“iPhone”导航中显示一个箭头。为此,我必须确定nav是否有任何子系统。我正在使用jquery进行尝试- $(document).ready(function(){ $("ul#cat_menu li").each(function(){ if($(this:has(ul))){ $(this).css('b

我有一个html文件中的动态树菜单。结构是-

这是一个垂直的树菜单。菜单没问题。但我想显示一个小箭头,如果导航有子导航。像第三个。我想在“iPhone”导航中显示一个箭头。为此,我必须确定nav是否有任何子系统。我正在使用jquery进行尝试-

$(document).ready(function(){
  $("ul#cat_menu li").each(function(){
    if($(this:has(ul))){
        $(this).css('background','red');//I will use code here for arrow.
    }
  })

});
但它不起作用。有什么帮助吗??提前感谢。

这是无效的JS代码†:

应该是:

$(this).has('ul')
// or alternatively
$(this).find('ul').length

†:似乎您想使用选择器。选择器必须始终为字符串。如果您已经有一个对象的引用,例如使用
this
,那么您需要找到相应的方法,并在
$(this)

上调用此方法

$("ul#cat_menu li").each(function(){
    if($('ul',this).length){
        $(this).css('background','red');//I will use code here for arrow.
    }
})

您的整个实现可以替换为以下内容:

// nothing happens if there are no matches, no need for a condition
// or the `.each` since ul#cat_menu li's are implicitly iterated
$("ul#cat_menu li").find('ul > li').css("border", "1px solid red");

演示:

@Rudie:是的,但它调用
$(这个)。查找('ul')。长度(内部)。我认为是另一种方式,因为Sizzle就是这样工作的,但还好。@Rudie:不是在这种情况下,请看这里:哈哈kewl好的,我确信=)我从
1.3.2
// nothing happens if there are no matches, no need for a condition
// or the `.each` since ul#cat_menu li's are implicitly iterated
$("ul#cat_menu li").find('ul > li').css("border", "1px solid red");