35

示例代码

<a href="page" style="text-decoration:none;display:block;">
    <span onclick="hide()">Hide me</span>
</a>

由于a标签超出了跨度,因此无法单击它。我尝试了z-index但这没有用

4

6 回答 6

60
<a href="http://the.url.com/page.html">
    <span onclick="hide(); return false">Hide me</span>
</a>

这是最简单的解决方案。

于 2013-09-14T18:40:25.357 回答
11

当您单击 时hide me,会触发aspan单击。由于页面正在重定向到另一个页面,因此您看不到hide()

您可以查看此内容以获得更多说明

http://jsfiddle.net/jzn82/

于 2012-05-02T11:43:47.113 回答
4

找到答案。

我使用了一些样式来实现这一点。

<span 
   class="pseudolink" 
   onclick="location='https://jsfiddle.net/'">
Go TO URL
</span>

.pseudolink { 
   color:blue; 
   text-decoration:underline; 
   cursor:pointer; 
   }

https://jsfiddle.net/mafais/bys46d5w/

于 2016-03-31T16:41:19.113 回答
0

使用onmouseup

尝试这样的事情

        <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>

编辑:

          <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>
于 2012-05-02T11:45:05.487 回答
-1

我会使用 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();
     }
}
于 2012-05-02T11:43:01.543 回答
-2

你可以使用 jQuery

http://jsfiddle.net/Bdqv7/

于 2012-05-02T11:44:21.717 回答