数据切换和onclick不能一起工作,我太新了,无法应用@epascarello的解决方案

Data-toggle and onclick doesn't work together and i'm too new to apply @epascarello 's solution to

本文关键字:应用 解决方案 @epascarello onclick 不能 工作 一起 数据      更新时间:2023-09-26

我有一个有点像这样的问题:

jQuery onclick不适用于具有数据切换和数据目标的按钮

但是我无法将解决方案从@epascarello应用于我的问题

我正在使用引导程序,我想在同一<上添加折叠和点击操作>

<a class="control" data-toggle="collapse" data-target="#demo"onclick="play('audioPlayer', this)">

如果我删除数据切换部分,则 onclick 有效,如果我删除 onclick 部分,则数据切换有效,

谁能帮我?

谢谢:)

首先给你的a tag一个 id

<a id="YourID" class="control" data-toggle="collapse" 
data-target="#demo"onclick="play('audioPlayer', this)">

并且比你的 JS

$(document).on("click", "#YourID", function() {
  alert("Test");
});

先试试这个

这称为委托事件处理程序。您可以参考: http://api.jquery.com/on/

解释

使用委托事件处理程序,可以将event handlers附加到运行代码时存在的 parent 元素(在本例中为document)。单击子元素(无事件处理程序)时,事件由浏览器冒泡,并由其父元素处理附加了事件处理程序

委托事件的优点是它们可以处理来自 稍后添加到文档中的后代元素。由 选取在 附加了委托事件处理程序,您可以使用委托事件来 避免频繁附加和删除事件处理程序的需要

相关文章: