2012-12-07 120 views
1

是否有可能为我生成的HTML类的选择器语句我想在JavaScript中操作。我发现这个类是存在的,但是当我尝试使用top时。$ find('。specific_class');它返回null,这让我相信我正在使用不正确的select语句。有没有办法在铬检查器生成DOM选择器

+0

你应该使用'$(”。specific_class')'方法find('。specific_class')' – koopajah

+0

如果您在HTML视图中选择一个元素,然后在控制台中选择一个元素你可以把它称为'$ 0'。为了把它包装在一个jQuery对象中,你可以做'$($ 0)'。 – Pointy

回答

1

您的JQuery选择器格式不正确。

尝试以下操作:

$(".specific_class").find(function(){     

     // do your code here with this instance of the found .specific_class $(this) 

     // get this items ID 
     var id = $(this).attr("id"); 


}); 
+0

那么现在我可以使用函数(item){alert(item.id)是否正确?或者我会使用item.attr('id')?我正在尝试返回特定类的唯一标识。非常感谢您的帮助! – Reaper

+1

在答案中增加了示例代码以获取物品'id' – Darren

2

您可以通过使用.length测试,如果选择存在 - 例如:

if($(".specific_class").length){ 
    //$(".specific_class") definitely exists, so now you can do something like: 
    $(".specific_class").find(function(){ 
     //code 
    }); 
} 
+0

不需要首先测试它的长度 - 如果找不到任何“find”,它将不会触发。 – Darren

相关问题