简体   繁体   English

仅在单击的按钮上切换减号/加号...?

[英]Make the minus/plus sign toggle only on the clicked button...?

How can I make the minus/plus sign toggle only on the clicked button?如何仅在单击的按钮上切换减号/加号? How can I make it clickable (it's not right now)...我怎样才能让它可点击(现在不是)...

JAVASCRIPT爪哇脚本

$(".dropbtn").append('<span class="adl-signs">+</span>');

function ctaDropMenu(e) {
  e.target.nextElementSibling.classList.toggle("show");
}

function toggleSigns() {
  $(".adl-signs").html(function(_, html) {
    return $.trim(html) == '+' ? '-' : '+';
  });
}

$(".dropbtn").click( function(e) {
    ctaDropMenu(e)
  toggleSigns()
});

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

https://jsfiddle.net/neuhaus3000/jf1zetLw/4/ https://jsfiddle.net/neuhaus3000/jf1zetLw/4/

Change those two functions like this:像这样更改这两个函数:

function toggleSigns(e) {
  $(e.target).find(".adl-signs").html(function(_, html) {
    return $.trim(html).slice(-1) == '+' ? '-' : '+';
  });
}

$(".dropbtn").click( function(e) {
  ctaDropMenu(e)
  toggleSigns(e)
});

And try to use different id values for elements.并尝试为元素使用不同的 id 值。 Two div's got id="myDropdown" .两个 div 有id="myDropdown"

Why don't you simplify things and use css to add the + instead of jQuery?为什么不简化事情并使用 css 添加+而不是 jQuery?

.dropbtn:after{
    content: '+';
}

.dropbtn.open:after{
    content: '-';
}

Then you can just toggle the open class on .dropbtn然后你可以在.dropbtn上切换open

$(".dropbtn").click( function(e) {
    e.target.classList.toggle("open");
    e.target.nextElementSibling.classList.toggle("show");
});

See the forked jsFiddle: [Link] ( https://jsfiddle.net/e1x10ae6/ )查看分叉的 jsFiddle:[链接] ( https://jsfiddle.net/e1x10ae6/ )

To answer why it was changing the +/- sign on both buttons, you gave the same class to both spans: $(".dropbtn").append('<span class="adl-signs">+</span>');为了回答为什么它改变了两个按钮上的 +/- 符号,你给了两个跨度相同的类: $(".dropbtn").append('<span class="adl-signs">+</span>'); Then in toggleSigns function you selected all instances of that class ( .adl-signs ) when you used the following selector $(".adl-signs") and changed the sign on all.然后在toggleSigns函数中,当您使用以下选择器$(".adl-signs")并更改所有.adl-signs时,您选择了该类的所有实例 ( .adl-signs )。

Add a unique id on each of your button like this:在每个按钮上添加一个唯一的id ,如下所示:

<button id="1" class="dropbtn">Click Me</button>

Then use that id to specfify which button to change然后使用该 ID 指定要更改的按钮

$(".dropbtn").click( function(e) {
    var btn_id = $(this).attr('id'); //get button id
    ctaDropMenu(e)
    toggleSigns(btn_id)
});

On your toggleSigns function use the id to select the button在您的toggleSigns功能上,使用 id 选择按钮

function toggleSigns(id) {
    $("#"+id+" .adl-signs").html(function(_, html) {
       return $.trim(html) == '+' ? '-' : '+';
    });
}

Updated fiddle: jsfiddle更新的小提琴: jsfiddle

As Veve said, you are using the class to do your toggle.正如 Veve 所说,您正在使用该类进行切换。 However, if you pass this (the element being clicked) you can target the button being used.但是,如果您传递this (被点击的元素),您可以定位正在使用的按钮。

function toggleSigns(element) {
  $(element).find('.adl-signs').html(function(_, html) {
    return $.trim(html) == '+' ? '-' : '+';
  });
}

$(".dropbtn").click( function(e) {
    ctaDropMenu(e)
  toggleSigns(this)
});

updated fiddle: https://jsfiddle.net/jf1zetLw/7/更新小提琴: https : //jsfiddle.net/jf1zetLw/7/

e: updated with real fiddle :/ e:用真正的小提琴更新:/

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

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