如何使跨度完全可点击?
我想让一个跨度完全可点击。
<span><a href="#" title="Remove" id="159" class="remove">159</a><input type="hidden" name="t[1][p][]" value="159"></span>
该跨度被分配了一个向右浮动的背景图像。该图像是一个加号,基本上用于指示记录可以移动到另一个 div。
是否可以简单地让整个跨度充当锚点,而不是让链接和图像都可点击?
单击必须由 jQuery 检测到。
I'd like to make a span fully clickable.
<span><a href="#" title="Remove" id="159" class="remove">159</a><input type="hidden" name="t[1][p][]" value="159"></span>
The span is assigned a background image which is floated to the right. The image is a plus sign which will basically serve to indicate the record can be moved to another div.
Rather than having a link and an image both clickable, is it possible to simply have the whole span act like a anchor?
The click must be detected by jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
HTML:
脚本:
HTML:
Script:
使锚块水平(即显示:块),假设您的跨度本身具有定义的大小,它将填充跨度(否则它只会包裹锚点,并且它们的大小相同)
Make the Anchor block level (ie display:block) and it will fill the span, assuming your span itself has a defined size (otherwise it will just wrap the anchor and they will both be the same size)
$("a.remove").closest("span").click(function(){alert('Test');});
就可以了。顺便说一下,将 id 设置为纯数字值并不是一个好主意。 (您将
a
元素的id
设置为“159”)。这会使您的标记无效,因为任何标识符都必须以拉丁字符开头,后跟字符、数字、破折号等。$("a.remove").closest("span").click(function(){alert('Test');});
will do.By the way, it's not a good idea to set the id to a numeric-only value. (You have the
id
of thea
element set to "159"). This makes your markup invalid, as any identifier has to start with a latin character followed by characters, digits, dashes etc.这也会触发链接的点击事件。
This will fire the link's click event too.
http://jsfiddle.net/mDLwZ/1/
我认为以下事实存在问题:当您单击跨度时,由于某种原因它会单击内部的 href,这里有一个小解决方法。
http://jsfiddle.net/mDLwZ/1/
I think there's a problem with the fact that when you click the span, it clicks the href inside for some reason, here's a small work around.