简体   繁体   English

如何完美更改加号和减号以显示打开和关闭图层

[英]how to change plus and minus sign perfectly for showing open and close layers

在此处输入图像描述

Hello I have created a list, for showing product and it's detail, But am unalbe to change minus and plus sign when I open a layers "plus" changing into minus but after one more click it's not changing in "plus" sign again.您好,我已经创建了一个列表,用于显示产品及其详细信息,但是当我打开一个“加号”变为减号的图层时,我无法更改减号和加号,但再单击一次后,它不会再次更改为“加号”。 how to do that plz see image for better understanding.如何做到这一点请看图片以便更好地理解。

 $('#layername').click(function() { $('#layerDetail').slideToggle(); $('#layerDetail2').slideUp(); $('#layerDetail3').slideUp(); }) $('#layername2').click(function() { $('#layerDetail2').slideToggle(); $('#layerDetail1').slideUp(); $('#layerDetail').slideUp(); }) $('#layername3').click(function() { $('#layerDetail3').slideToggle(); $('#layerDetail').slideUp(); $('#layerDetail2').slideUp(); }) $('.SLayer').click(function() { x = $(this).next('li'); if ($(this).next('li').css('display') == 'none') { $(this).find('span:first').text('+') } else { $(this).find('span:first').text('-') } });
 * { box-sizing: border-box } body { font-family: "Lato", sans-serif; }.SLayer { background: #eee; padding: 10px; cursor: pointer; }
 <ul style="list-style: none; "> <li class="SLayer" id="layername"> <span style="font-weight: 600; color:black">+</span>&nbsp;<span> Car </span> </li> <li class="SLayer" id="layerDetail" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> <li class="SLayer" id="layername2"><span style="font-weight: 600; color:black">+</span>&nbsp;<span> Bus </span> </li> <li class="SLayer" id="layerDetail2" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> <li class="SLayer" id="layername3"><span style="font-weight: 600; color:black">+</span>&nbsp;<span> Train</span> </li> <li class="SLayer" id="layerDetail3" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> </ul>

I've modified your code a bit.我稍微修改了你的代码。 You can try this:你可以试试这个:

$('li[id^=layername]').click(function() {
  var t = $(this).next();
  $("span:first",this).text($("span:first",this).text() == "+" ? "-" : "+")
  t.slideToggle();
  $('li[id^=layername]').not(this).find("span:first").text('+');
  $('li[id^=layerDetail]').not(t).slideUp();
})

Demo演示

 $('li[id^=layername]').click(function() { var t = $(this).next(); $("span:first",this).text($("span:first",this).text() == "+"? "-": "+") t.slideToggle(); $('li[id^=layername]').not(this).find("span:first").text('+'); $('li[id^=layerDetail]').not(t).slideUp(); })
 * { box-sizing: border-box } body { font-family: "Lato", sans-serif; }.SLayer { background: #eee; padding: 10px; cursor: pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul style="list-style: none; "> <li class="SLayer" id="layername"> <span style="font-weight: 600; color:black">+</span>&nbsp;<span> Car </span> </li> <li class="SLayer" id="layerDetail" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> <li class="SLayer" id="layername2"><span style="font-weight: 600; color:black">+</span>&nbsp;<span> Bus </span> </li> <li class="SLayer" id="layerDetail2" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> <li class="SLayer" id="layername3"> <span style="font-weight: 600; color:black">+</span>&nbsp;<span> Train</span> </li> <li class="SLayer" id="layerDetail3" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> </ul>

Some suggestions:一些建议:

  1. it would be better to use CSS for Plus and minus icon, not the HTML,最好将 CSS 用于加号和减号图标,而不是 HTML,
  2. use one LI for one item and keep 2 different div or H or P inside that LI, so here should be 3 LI not 6 for this structure, This can vary but for me, that would be the ideal one.对一个项目使用一个 LI,并在该 LI 中保留 2 个不同的 div 或 H 或 P,所以对于这种结构,这里应该是 3 个 LI 而不是 6 个,这可能会有所不同,但对我来说,那将是理想的。
  3. don't write code for every click, use modular code so that if the content increase or decrease then you do not need to change the jQuery code不要每次点击都编写代码,使用模块化代码,这样如果内容增加或减少,则无需更改 jQuery 代码

 $('.header').click(function() { $(".header").not(this).next().slideUp(); $(this).next().slideToggle(); $(".header").not(this).removeClass("opened"); $(this).toggleClass("opened"); })
 * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: "Lato", sans-serif; }.SLayer { background: #eee; padding: 10px; cursor: pointer; }.header {padding-left: 30px;position: relative;}.header:before, .header:after { content: ""; display: block; position: absolute; height: 1px; width: 10px; background: red; top: 50%; left: 14px; transform: translateY(-50%); }.header:before { transform: translateY(-50%) rotate(90deg); }.header.opened:before { opacity: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul style="list-style: none; "> <li class="SLayer header" id="layername"> Car </li> <li class="SLayer" id="layerDetail" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> <li class="SLayer header" id="layername2"> Bus </li> <li class="SLayer" id="layerDetail2" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> <li class="SLayer header" id="layername3"> Train </li> <li class="SLayer" id="layerDetail3" style="background:blue; padding: 20px; color: white; display: none">Car Details</li> </ul>

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

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