简体   繁体   English

在切换时更改锚标记内的文本?

[英]Change text inside anchor tag on toggle?

I have the following: 我有以下内容:

在此处输入图片说明

So right now I'm toggling ul.children like this: 所以现在我要像这样切换ul.children

$('a.expand-cat-item').on('click', function() {
  $(this).closest('li.cat-item').find('ul.children').toggle('slow');
});

Now, I would also change the + sign into a - sign inside a.expand-cat-item (which will work as a toggle too). 现在,我还将a.expand-cat-item (也可以用作切换)内的+号更改为-号。

How to accomplish that? 如何做到这一点?

You can use condition operator ? : 您可以使用条件运算符? : ? : to make a text switch condition. ? :设置文本切换条件。

$('a.expand-cat-item').on('click', function() {
  $(this).closest('li.cat-item').find('ul.children').toggle('slow');
  $(this).text($(this).text() == "+" ? "-" : "+");
});

Try 尝试

$('a.expand-cat-item').on('click', function() {
  $(this).closest('li.cat-item').find('ul.children').toggle('slow');
  if ($(this).text() == '+ '){
      $(this).text('- ');
  }
  else{
      $(this).text('+ ');
  }
});

用这个:

$('a.expand-cat-item').text("- ");
$('a.expand-cat-item').on('click', function() {
  var $this = $(this);
  $this.closest('li.cat-item').find('ul.children').toggle('slow');
   var html = '+';
   if($this.html().indexOf('+') > -1){
       html = '-';
   }
   $this.html(html);
});

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

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