简体   繁体   English

如果单击两次,如何打开和关闭div

[英]How to open and close a div if its clicked on Twice

This is my jsfiddle 这是我的jsfiddle

http://jsfiddle.net/1ty1v8u1/5/ http://jsfiddle.net/1ty1v8u1/5/

If an element is clicked on twice , how can i achieve toggle behavior for that particular div ? 如果两次单击某个元素,如何实现该特定div的切换行为?

If i click on office twice in the jsfiddle how to make it open and close ?? 如果我在jsfiddle中两次单击Office ,如何使其打开和关闭?

This is my code 这是我的代码

function showRestaurantDetailsByLocation(response,locationname)
{
    $('.restListings').remove();
    $('.addNewRestaurant').remove();
    var ulhtml = $('<ul class="restListings"></ul>');
    var divhtml = $('<div class="inner-intit"><sub class="sub">Your Favorite Area</sub></div>');
    divhtml.append('<br>');
    var $newbutton= $('<input/>').attr({ type: 'button', location:locationname , name:'btn1', class:'btn btn-success addNewRestaurant', value:locationname});
    for(var i=0;i<response.length;i++)
    {
        divhtml.append('<li><h6>'+response[i].area+'</h6><p>'+response[i].address+'</p><span id="delete" class="inDelete inDeleteSub"></span></li>');
    }
    divhtml.append($newbutton); 
    ulhtml.append(divhtml);
    $("#"+locationname).append(ulhtml);
}

You are appending the new elements on click. 您将在单击时附加新元素。 Just check for previously appended element in same div. 只需检查同一div中以前添加的元素即可。 If it exist then simply remove it, if not then add new: 如果存在,则将其删除,如果不存在,则添加新的:

 $(document).on('click', '.lielement', function() {
   var locationname = $(this).attr("id");
   if($(this).find('.restListings').length)
     $(this).find('.restListings').remove()
   else
     displayingRestaurantByArea(locationname);
 });

Working Demo 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM