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

Javascript 如果两次单击div,如何打开和关闭div,javascript,jquery,Javascript,Jquery,这是我的护照 如果一个元素被点击两次,我如何实现该特定div的切换行为 如果我在JSFIDLE中两次单击office,如何打开和关闭它 这是我的密码 function showRestaurantDetailsByLocation(response,locationname) { $('.restListings').remove(); $('.addNewRestaurant').remove(); var ulhtml = $('<ul class="rest

这是我的护照

如果一个元素被点击两次,我如何实现该特定div的切换行为

如果我在JSFIDLE中两次单击office,如何打开和关闭它

这是我的密码

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);
}
函数showRestaurantDetailsByLocation(响应、位置名称)
{
$('.restListings').remove();
$('.addNewRestaurant').remove();
var ulhtml=$('
    ); var divhtml=$(“您最喜欢的区域”); 追加(“
    ”); var$newbutton=$('').attr({type:'button',location:locationname,name:'btn1',class:'btn btn success addNewRestaurant',value:locationname});
    对于(var i=0;i您在单击时添加新元素。只需检查同一div中以前添加的元素。如果它存在,则只需删除它,如果不存在,则添加新元素:

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

    使用
    .toggle()
    函数交替隐藏和显示DIV。已经这样做了,但是它删除了整个元素。删除了什么整个元素?它应该只切换您指定的元素,所以您必须切换错误的元素。@PreethiJain:很高兴它有帮助:)如果您不介意的话,还有一个问题,我如何向JSFIDLE“add For”中的按钮添加一些静态文本。因此它看起来像是添加位置。@PreethiJain:您想在哪里添加它。在fiddle中,当单击office时,会添加一个新按钮。您知道,我需要在位置名称之前添加一些静态内容,以便它看起来像一个s为添加新位置locationname@PreethiJain:哦,太好了。我只是想解决这个问题。:)