我有一个问题有点像这个:
jQuery onclick doesn't work on a button with data-toggle and data-target
但我无法将@epascarello 的解决方案应用于我的问题
上添加折叠和点击操作<a class="control" data-toggle="collapse" data-target="#demo"onclick="play('audioPlayer', this)">
如果我删除数据切换部分,onclick 工作,如果我删除 onclick 部分,数据切换工作,
谁能帮帮我?
谢谢你:)
最佳答案
首先给你的a标签
<a id="YourID" class="control" data-toggle="collapse"
data-target="#demo"onclick="play('audioPlayer', this)">
比你的 JS
$(document).on("click", "#YourID", function() {
alert("Test");
});
先试试这个
这称为委托(delegate)事件处理程序。您可以引用:http://api.jquery.com/on/
解释
使用委托(delegate)事件处理程序,您将事件处理程序
附加到我们运行代码时存在的parent
元素(它是document
在这种情况下)。单击子元素时(无事件处理程序),事件由浏览器冒泡并由其父元素处理(附加事件处理程序>)
Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers
关于javascript - Data-toggle 和 onclick 不能一起工作,我太新了,无法将@epascarello 的解决方案应用于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35034394/