如何在最近添加的上绑定点击事件 jquery 中的标签
我在一个页面上有 6 个指向 mp3 的链接。
我安装的插件用 swf 替换这些链接并内联播放该 mp3。
我遇到的问题是可以激活所有 6 个链接并同时播放所有音频。 我解决了这个问题(虽然我感觉是一种笨拙的新手方式),方法是在 标记被替换为嵌入标记之前捕获它,然后在替换时将其放回原处。另一个被点击。
但是,现在这是我的问题:我放回的 标签会丢失它的 onClick 事件(我认为这就是正在发生的事情),因此,如果单击第二次一次,无法像第一次那样切换。
$(".storyplayer a").bind("click", function() {
// replace the <a> tag from the previous media player installation
if (previousplayerlocation != null) {$("#" + previousplayerlocation + " .storyplayer").html(graboldcode);}
// now remember this installation's <a> tag before it's replaced
graboldcode = $(this).parent().html();
// remember where I grabbed this <a> tag from
previousplayerlocation = $(this).parents("div.storylisting").attr("id");
// replaces the <a> tag with the media player.
$(this).media({width: 190,height: 30});
return false;
});
有没有办法将点击事件重新分配给 if
语句?
I have 6 links on a page to an mp3.
The plugin I installed replaces those links with a swf and plays that mp3 inline.
The problem I had was that it was possible to activate all 6 links and have all audio playing at once. I solved that problem (I feel in a clumsy novice way though) by catching the <a>
tag before it was replaced with the embed tag and then putting it back when another one was clicked.
However, this is my problem now: the <a>
tag I put back looses it's onClick event (i think that's what is happening) and so, if clicked a second time, fails to switch like the first time.
$(".storyplayer a").bind("click", function() {
// replace the <a> tag from the previous media player installation
if (previousplayerlocation != null) {$("#" + previousplayerlocation + " .storyplayer").html(graboldcode);}
// now remember this installation's <a> tag before it's replaced
graboldcode = $(this).parent().html();
// remember where I grabbed this <a> tag from
previousplayerlocation = $(this).parents("div.storylisting").attr("id");
// replaces the <a> tag with the media player.
$(this).media({width: 190,height: 30});
return false;
});
Is a way to re-assign the click event to the if
statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用事件委托 - 这意味着将点击绑定到某个容器并让其处理事件。 然后,您可以查询 event.target 以查看它是否是被单击的锚点,然后您是否需要行为。 出于多种原因,这更好。
代码类似于
查看此 post< /a> 更多阅读
Use event delegation - this means binding the click to some container and let that handle the event. You can then query the event.target to see if it was an anchor that was clicked then do you required behaviour. This is better for a number of reasons.
The code would be something like
See this post for more reading
丢失 onclick 处理程序,因为它被删除。 当您重新添加 HTML 时,它不会返回该处理程序。
无需重新安排您的代码,您就可以创建一个函数来充当您的点击处理程序。
The <a> loses the onclick handler because it is being removed. When you re-add the HTML, it doesn't get back that handler.
Without rearranging your code much, you can create a function which will serve as your click handler.
查看 livequery 插件。 使用此插件,即使对于 DOM 中不可用的元素,您也可以绑定事件侦听器。
插件监视 DOM 并绑定侦听器。 您还可以自己强制评估:
一些其他强大的功能:
您可以使侦听器过期:
您可以注册 onmatch 侦听器:
Have a look at the livequery plugin. With this plug-in you can bind event listeners even for elements that aren't available in DOM ready.
The plug-in monitors the DOM and binds the listeners. You can also force the evaluation yourself:
Some other powerful features:
You can expire listeners:
You can register onmatch listeners: