javascript - Data-toggle 和 onclick 不能一起工作,我太新了,无法将@epascarello 的解决方案应用于

标签 javascript jquery twitter-bootstrap onclick

最佳答案

首先给你的a标签

一个id
<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/

相关文章:

javascript - 随机图像顺序幻灯片

javascript - 如何使用 Node 模块从ajax.inc.php页面获取数据?

javascript - 语法错误 : Unexpected Token <(. ..) - dojo/ajax

html - 如何创建带有 Logo 、搜索框和链接的响应式导航栏并且仍然响应?

javascript - 如何根据另一个div的高度更改一个div的最小高度

javascript - IE - select2 始终获得焦点

javascript - 如何使用 javascript 使数组内的按钮仅响应其自己的包装器?

javascript - 将 dropzone.js 与 fancybox.js 相结合以提供上传照片的全屏 View

javascript - Shadowbox 和 jQuery 在照片库方面的冲突

javascript - 框阴影和其他颜色效果不适用于 mozilla firefox