jQuery 如何使跨度完全可点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3659533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a span fully clickable?
提问by
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.
跨度分配了一个向右浮动的背景图像。图像是一个加号,基本上用于表示记录可以移动到另一个 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.
jQuery 必须检测到点击。
采纳答案by automagic
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)
使 Anchor 块级别(即 display:block),它将填充跨度,假设您的跨度本身具有定义的大小(否则它只会包裹锚点并且它们的大小相同)
回答by R G
HTML:
HTML:
<span id="myspan">....</span>
Script:
脚本:
$("#myspan").click(function(){
alert('I got a click');
});
回答by naivists
$("a.remove").closest("span").click(function(){alert('Test');});
will do.
$("a.remove").closest("span").click(function(){alert('Test');});
会做。
By the way, it's not a good idea to set the id to a numeric-only value. (You have the id
of the a
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.
顺便说一下,将 id 设置为仅数字值并不是一个好主意。(您id
将a
元素的 设置为“159”)。这会使您的标记无效,因为任何标识符都必须以拉丁字符开头,后跟字符、数字、破折号等。
回答by simplyharsh
$('a.remove').closest('span').click(function(){
$('a.remove', this).trigger('click');
})
This will fire the link's click event too.
这也会触发链接的点击事件。
回答by RobertPitt
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.
我认为当您单击跨度时,它会出于某种原因单击内部的 href,这是一个问题,这里有一个小的解决方法。