tr不包含jQuery的子代

时间:2014-04-08 12:26:15

标签: jquery tr

我有一个表行,其中#context_menu是最后一项。因为我想检查tr不是标题行。即tr不得有孩子th

$("#context_menu li").click(function(){
        var tr1 = $(this).parents("tr");
            var parent_tr = tr1.prev();
            //Here the code goes to check  is not a header row.
            //Since I want something like this
             if($( parent_tr+":contains('th')" ).length == 0)
             {
               alert("found desired row")
             }   
    })
}

2 个答案:

答案 0 :(得分:1)

parent_tr是一个jQuery对象,使用find()

var parent_tr = tr1.prev();
//Here the code goes to check  is not a header row.
//Since I want something like this
if (parent_tr.find("th").length == 0) {
    alert("found desired row")
}

答案 1 :(得分:1)

使用.find()查看selector.try中出现的元素:

       $("#context_menu li").click(function(){
        var tr1 = $(this).closest("tr");
        var parent_tr = tr1.prev();
        if(!$( parent_tr ).find('th').length)//or $( parent_tr ).find('th').length == 0
        {
          alert("found desired row")
        }
       })};