热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

单击图像时如何使用JQUERY选择第二个元素?-HowtoselectsecondelementwithJQUERYwhenclickedonimage?

Thecodebelowworkingforonlyfirstdiv.Icantdisplayotherdivswheniclickedontheotheri

The code below working for only first div. I can't display other divs when i clicked on the other "img"s. Do you know a method to reach second (next) element?

以下代码仅适用于第一个div。当我点击其他“img”时,我无法显示其他div。你知道一种方法来达到第二个(下一个)元素吗?

$(document).ready(function() {

        $('.imgOpenClose').click(function() {
            if ($(this).attr("src") == "images/openTree.gif") {
                $(this).attr("src", "images/closeTree.gif");
                $(this).parent().find('div').eq(0).show();
            }
            else {
                $(this).attr("src", "images/openTree.gif");
                $(this).parent().find('div').eq(0).hide();
            }
        });
});

Wanna open table when i clicked on plus images

And wanna see the table inside this div

2 个解决方案

#1


Since you have a break element following the image, you'll need to use nextAll and select the first DIV instead of simply using next with a filter as in my original answer.

由于图像后面有一个break元素,因此您需要使用nextAll并选择第一个DIV,而不是像我原来的答案那样只使用next和过滤器。

$('.imgOpenClose').click( function() {
    $(this).nextAll('div:first').toggle();
});

#2


If you have several images with the same class-tag, you can still do it without having to append a unique id:

如果您有多个具有相同类标记的图像,您仍然可以执行此操作而无需附加唯一ID:

$(function() {
    $('.imgOpenClose').click(function() {
        $(this).next('div').children('table').toggle();
    });
});

推荐阅读
author-avatar
_Terr1鄭x宜_F
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有