简体   繁体   English

防止锚点跟随href

[英]Prevent an anchor from following href

I have an anchor defined as follows - 我有一个定义如下的锚点-

<a onClick="javascript:isLinked('');" class="..." href="..."><img border="0" alt="" height="8" width="8" src="..."/> <u>Add another</u></a> 

The javascript function - javascript函数-

function isLinked(sId) {
        if(!sId)
        {
            alert(".....");
            return false;
        }
        else
            document.body.style.cursor='wait';                  
    }

Although I do see the alert being triggered on the click of the anchor,the links still follows the target url. 尽管我确实看到通过单击锚点触发警报,但是链接仍然跟随目标URL。
How can i prevent the link from following? 如何防止链接跟随?

The browser is IE8(if that is relevant) 浏览器是IE8(如果相关)

您需要从onclick处理程序中返回方法返回的值

onClick="return isLinked('');"

The javascript way to disable a link is to add return false; 禁用链接的JavaScript方法是添加return false; . In your case, the boolean is determined by your function isLinked . 在您的情况下,布尔值由函数isLinked确定。 So the answer is : 所以答案是:

<a onClick="return isLinked('');" class="..." href="..."><img ...

In case that might be of use to you, there's also a css way of disabling links, as documented here . 如果您可能有用,还有一种禁用链接的CSS方法,如此处所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM