如果 HTML 标记没有提供 id,jQuery 中是否有东西可以唯一标识 DOM 节点,即 if $(NODE).attr("id")
返回 undefined
?
目标:排除特定类的特定节点。
jQuery(document).ready(function($){
$(".accordion-right-content").hide();
$(".article-image img").click(function() {
$(".accordion-right-content").each(function(i,v) {
// the following comparison does not work because of the missing ID
if ($(v).attr("id") !== $(this).parent().parent().children(".accordion-right-content").attr("id")) {
$(v).hide();
} else {
$(v).show();
}
});
});
除了特定的 $(this).parent().parent().children(".accordion-right-content") 在再次显示之前隐藏。
最佳答案
您应该像这样使用 not 运算符:
if (!$(NODE).attr("id")){
//do stuff here
}
下面的内容,你错了:
if ($(v).attr("id") !== $(this).parent().parent().children(".accordion-right-content").attr("id")) {
应该是这样的:
if ($(this).attr("id") !== $(this).parent().parent().children(".accordion-right-content").attr("id")) {
关于javascript - jQuery 虚拟 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074820/