单击链接时展开,第二次单击时隐藏

时间:2022-12-01 15:58:19

i have a script that is working but one way only.. I would like it to have it hide when the same link is clicked a second time.

我有一个工作但只有一种方式的脚本..我希望它在第二次点击相同的链接时隐藏它。

$('.EndPoint').on('click', function () {
$('.EndPoint').find('div').hide();
$(this).find('div').show();

Fiddle

-- UPDATE 1 --

- 更新1 -

Trying to work in this solution http://jsfiddle.net/ksvexr40/2/ but its now hiding properly.. two issues 1) only part of it is getting hidden 2) it starting off as expanded. I am using .append() to bring in the data and have many instances of them.. some example below

试图在这个解决方案中工作http://jsfiddle.net/ksvexr40/2/但它现在正确地隐藏了......两个问题1)只有部分内容被隐藏2)它从扩展开始。我正在使用.append()来引入数据并拥有它们的许多实例。下面是一些示例

                    var set = $('<div><a href="#" class="clicker"><h3>['+i+'] ' + endpoint1.ipAddress+ ' - ' +endpoint1.serverName+ '</h3></a></div>');
                $response0.append(set);

                if (endpoint1.serverName == null) {
                    var serverName = '<div class="hideThis"><b>Server Name:</b> n/a</div>';
                    set.append(serverName);
                    } else if (endpoint1.serverName != null) {
                        set.append('<div class="hideThis"><b>Server Name:</b> ' + endpoint1.serverName+'</div>');
                    } set.append('<div class="hideThis"><b>IP Address:</b> ' + endpoint1.ipAddress+ '<br></div>');

2 个解决方案

#1


1  

You can use .is(":visible") to check the visibility of an element:

您可以使用.is(“:visible”)来检查元素的可见性:

$('.EndPoint').on('click', function () {
   if($(this).find('div').is(":visible"))
   {
       $(this).find('div').hide();
   }else{       
       $(this).find('div').show();
   }
}

#2


1  

$(this).find('div').toggle(); Will do it

$(本).find( 'DIV')切换();会做的

$('.hideThis').hide();
$('.clicker').on('click', function () {
    $('.hideThis').not($(this).next()).hide();
    $(this).next().toggle();
});

http://jsfiddle.net/ksvexr40/2/

#1


1  

You can use .is(":visible") to check the visibility of an element:

您可以使用.is(“:visible”)来检查元素的可见性:

$('.EndPoint').on('click', function () {
   if($(this).find('div').is(":visible"))
   {
       $(this).find('div').hide();
   }else{       
       $(this).find('div').show();
   }
}

#2


1  

$(this).find('div').toggle(); Will do it

$(本).find( 'DIV')切换();会做的

$('.hideThis').hide();
$('.clicker').on('click', function () {
    $('.hideThis').not($(this).next()).hide();
    $(this).next().toggle();
});

http://jsfiddle.net/ksvexr40/2/