How to consume button click inside tag to prevent link being followed?

前端 未结 5 1963
傲寒
傲寒 2021-01-27 01:49

I\'ve added a button inside a link tag, with a little cross image, that I will eventually use to actually remove that element without it following the link also:

<
5条回答
  •  北荒
    北荒 (楼主)
    2021-01-27 02:19

    First change your function to:

    function detectClick(message){
        window.alert("Detected: " + message);
        return false; // add this line
    }
    

    then change the onclick handler to:

    onclick="return detectClick('Google')" // note the "return"
    

    Please do note that AFAIK the HTML standard does not allow to have a button inside an anchor.

提交回复
热议问题