简体   繁体   English

按钮中的图标未注册点击事件

[英]Icon in button not registering click event

I have a button with text and a down-arrow icon, which I use to trigger a dropdown.我有一个带有文本的按钮和一个向下箭头图标,我用它来触发下拉菜单。 When I click the text part of the button, the dropdown appears, and I have some jquery to amend the text and flip the arrow - great.当我单击按钮的文本部分时,会出现下拉菜单,并且我有一些 jquery 来修改文本并翻转箭头 - 很棒。 But when I click the icon part of the button, the text changes and the icon flips but the dropdown doesn't appear.但是,当我单击按钮的图标部分时,文本会更改并且图标会翻转,但不会出现下拉菜单。

Do I convert it to an anchor link and change the clickable area, or is there a more simple solution?我是否将其转换为锚链接并更改可点击区域,还是有更简单的解决方案?

HTML HTML

<button type="button" class="btn btn-info button-drop text-nowrap" data-toggle="collapse" data-target="#ph-detail" aria-expanded="false" aria-controls="ph-detail">See More 
<i class="bi bi-arrow-down"></i>
</button>

<div class="collapse" id="ph-detail">
<p>Expandable content</p>
</div>

JQuery JQuery

$('.button-drop').click(function() {
  $(this).toggleClass( "active" );
  if ($(this).hasClass("active")) {
    $(this).html('See Less <i class="bi bi-arrow-up">');
  } else {
    $(this).html('See More <i class="bi bi-arrow-down">');
  }
});

Both of the code are working for me.这两个代码都为我工作。 I think you have not added the cdnjs file of jQuery我想你没有添加 jQuery 的 cdnjs 文件

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>

 $('.button-drop').click(function() { $(".button-drop").toggleClass( "active" ); if ($(".button-drop").hasClass("active")) { $(".button-drop").html('See Less <i class="bi bi-arrow-up">'); } else { $(".button-drop").html('See More <i class="bi bi-arrow-down">'); } });

 $('.button-drop').click(function() { $(this).toggleClass( "active" ); if ($(this).hasClass("active")) { $(this).html('See Less <i class="bi bi-arrow-up">'); } else { $(this).html('See More <i class="bi bi-arrow-down">'); } });

In the end I solved it with the following:最后我用以下方法解决了它:

<button type="button" class="btn btn-info button-drop text-nowrap" data-toggle="collapse" data-target="#ph-detail" aria-expanded="false" aria-controls="ph-detail"><span>See More </span>
<i class="bi bi-arrow-down"></i>
</button>
$('.button-drop').click(function() {
    $(this).find('i').toggleClass('bi bi-arrow-up');
    $(this).find('span').text(function(a, text) {
        return text === "See More " ? "See Less " : "See More ";
    });
});

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

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