douzhuican0041 2019-05-15 07:11
浏览 79
已采纳

按钮内的HTML href(如chrome选项卡)1个按钮中的2个不同的单击功能

I want to have a X inside a button just like the X in the chrome tabs. So I have 4 buttons which will give me a POST when I click on 1 of them. Now I want to have a X icon inside that button to redirect to a different page/run a function

Example:

I tried to put a href inside the button but it doesn't work... the X will be blue like a href, but when i click on it, it will only click the button.

<button class="btn btn-default" style="background-color: #<?php echo $fontColor; ?>;width:240px;white-space:normal" type="submit" name="actief">
 <i class="fa fa-user fa-fw"></i>
 <?php 
 echo $result['FIRST_NAME'], " ".$result['LAST_NAME'];
 echo $icon; 
 ?>                                              
</button>
  • 写回答

1条回答 默认 最新

  • dousong5492 2019-05-15 08:44
    关注

    If i'm understanding correctly, you want an element nested inside of the button to be clickable and fire a function only when this element is clicked. You also want a separate action to run if the button itself has been clicked.

    You can run an event listener on the whole button then check if the element clicked via e.target was the button itself OR the X inside.

    If i have misinterpreted your question please let me know and i will try and correct my answer.

    const button = document.querySelector('button');
    const buttonClose = document.querySelector('.button__close');
    
    button.addEventListener('click',(e)=> {
      if(e.target.classList == buttonClose.classList){
        alert('Close Clicked')
      }
    });
    .button__close{
      background:white;
      padding:10px;
      display:inline-block;
      margin-left:5px;
    }
    <button>Example<span class='button__close'>x</span></button>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符