绑定事件到文本节点 [英] Binding event to text node
本文介绍了绑定事件到文本节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的HTML。我需要将点击事件绑定到someText
Here's my HTML. I need to bind a click event to "someText"
<div id="container">
someText <a href="#">A link</a>
</div>
someText可以是任何文本字符串
"someText" could be any string of text
推荐答案
经过一阵混乱,我找到了一个比Patrick更好的解决方案。在这种情况下,它可以在链路之后选择节点,使其更全面地可用。可能更好地在这里发布:)
After a bit of messing I found a slightly better solution to Patrick's. It can select nodes after the link in this situation, making it more univerally usable. Probably better posting it here :)
$('#container')
.contents()
.filter(function() {
return this.nodeType == Node.TEXT_NODE;
})
.wrap('<span/>')
.parent()
.click(function(){
alert($(this).text());
});
这篇关于绑定事件到文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文