Html 跨标签内的 onclick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10412883/
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
span with onclick event inside a tag
提问by Ben
Sample code
示例代码
<a href="page" style="text-decoration:none;display:block;">
<span onclick="hide()">Hide me</span>
</a>
Since the atag is over the span is not possible to click it. I try z-indexbut that didnt work
由于a标记超过跨度,因此无法单击它。我尝试了z-index但这没有用
回答by Mark
<a href="http://the.url.com/page.html">
<span onclick="hide(); return false">Hide me</span>
</a>
This is the easiest solution.
这是最简单的解决方案。
回答by Nauphal
When you click on hide me
, both aand spanclicks are triggering. Since the page is redirecting to another, you cannot see the working of hide()
当您单击 时hide me
,会触发a和span单击。由于页面正在重定向到另一个页面,因此您无法看到hide()
You can see this for more clarification
您可以查看此内容以获得更多说明
回答by MAFAIZ
Fnd the answer.
寻找答案。
I have use some styles inorder to achive this.
我使用了一些样式来实现这一点。
<span
class="pseudolink"
onclick="location='https://jsfiddle.net/'">
Go TO URL
</span>
.pseudolink {
color:blue;
text-decoration:underline;
cursor:pointer;
}
回答by srini
use onmouseup
在鼠标上使用
try something like this
尝试这样的事情
<html>
<head>
<script type="text/javascript">
function hide(){
document.getElementById('span_hide').style.display="none";
}
</script>
</head>
<body>
<a href="page" style="text-decoration:none;display:block;">
<span onmouseup="hide()" id="span_hide">Hide me</span>
</a>
</body>
</html>
EDIT:
编辑:
<html>
<head>
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function () {
$(this).fadeTo("fast", .5).removeAttr("href");
});
});
function hide(){
document.getElementById('span_hide').style.display="none";
}
</script>
</head>
<body>
<a href="page.html" style="text-decoration:none;display:block;" onclick="return false" >
<span onmouseup="hide()" id="span_hide">Hide me</span>
</a>
</body>
</html>
回答by Jordan
I would use jQuery to get the results that you're looking for. You wouldn't need to use an anchor tag at that point but if you did it would look like:
我会使用 jQuery 来获得您正在寻找的结果。那时您不需要使用锚标记,但如果您这样做,它看起来像:
<a href="page" style="text-decoration:none;display:block;">
<span onclick="hide()">Hide me</span>
</a>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.2.min.js' /
<script type='text/javascript'>
$(document).ready(function(){
$('span').click(function(){
$(this).hide();
}
}