简体   繁体   中英

How to disable anchor tag after first click in javascript ?

I have a a href tag with css class thickbox I need to disable the link after first click. how I can do this?

Aspx code

<a href="SomePage.aspx?FDID=11&KeepThis=true&TB_iframe=true&height=150&width=400"
     onclick="return DoSomething(this)" class="thickbox" id="AnchorID">
<img id="MyImageButton" alt="Image" src="SiteImages/image.png" runat="server" />
</a>

My JavaScript Method

function DoSomething(element) {

                return true;
            }

In your javascript you can set the attribute disabled="" at your element. (look here )

If, with disable, you mean to not let the browser go to the url, you have to call event.preventDefault() (look here )

For this you have to create a hidden feild which will store a value example 1 or 0. If the user clicks the link first time set the hidden feild value to 1 and allow the user to redirect and then disable the hyperlink. If user again clicks the hyper link then first check the hidden feild if it contains a value 1 then don't allow him to redirect. Hope you understand my point. Also set the href property in the javascript.

I'm not sure exactly what the OP is trying to do, but if it is purely to allow the script to run the once after clicking, something like this would work...

function DoSomething(element) {
  if(element.getAttribute("block")==null){
    ...
    element.setAttribute("block","1");
    return true;
  }else{
    return false;
  }
}

This has the advantage of not changing the style of button by setting the disabled attribute.

However, returning true from the function and then in turn returning true to the OnClick handler of the link will result in the browser navigating to the URL in href ... so I can't see why the need for the blocking of a 2nd run. (Unless thickbox , which I know nothing about, works in a way which is not the normal.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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