javascript - jQuery 虚拟 ID?

标签 javascript jquery html

如果 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/

相关文章:

javascript - 如何在 "Complete page load"和 "after all external script execution"之后调用函数?

javascript - 根据数据库结果填充li内容

html - 字体未加载,但 URL 指向正确

javascript - Jquery 中的 Div 位置

javascript - 数据库中的日期在前端被视为String

jquery - 在 jQuery mobile 中创建侧边栏

jquery - 如何修改 Magnific Popup 中的关闭按钮行为?

javascript - 为无序项目分配编号

javascript - RxJS:与多个单独的可观察量相比,单个可观察量有什么缺点吗?

javascript - 如何在下拉列表中选择完整的日历月份和年份