Javascript 如何使用 Jquery 隐藏锚链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4550425/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:55:02  来源:igfitidea点击:

How to hide anchor link with Jquery

javascript

提问by user545520

Possible Duplicate:
Disable anchor link failed with javascript

可能的重复:
使用 javascript 禁用锚链接失败

I have anchor tag

我有锚标签

<a id="click" href="javascript:void(0);" onclick="disable();" style="">Clickme</a>.

When html loads it showing clickme,when i click anchor link,it calls disable() function,In disable function i am trying to hide it using below code within the jquery success function:

当html加载它显示clickme时,当我单击锚链接时,它调用disable()函数,在禁用函数中,我试图在jquery成功函数中使用以下代码隐藏它:

document.getElementById("click").style.display='none';

But page never hides that anchor link,Please let me know if i am doing anything wrong.

但是页面从不隐藏该锚链接,如果我做错了什么,请告诉我。

Thanks in advance.

提前致谢。

回答by Mohamed Faramawi

if you have the id of the anchor you can use $('#ID').hide()to hide it and to show it use $('#ID').show()

如果你有锚的 id,你可以$('#ID').hide()用来隐藏它并显示它使用$('#ID').show()

回答by rahul

Since you are using jQuery, you can follow unobtrusive way of coding

由于您使用的是 jQuery,您可以遵循不显眼的编码方式

jQuery

jQuery

$(function(){
    $("#click").click(function(){
        $(this).hide();
        return false;
    });
});

HTML

HTML

<a id="click" href="#">Clickme</a>

See a working demo

查看工作演示

If this is not working then check your HTML. If there is more than one element with the same id then the HTML will be invalid and your script wont work properly.

如果这不起作用,请检查您的 HTML。如果有多个具有相同 id 的元素,则 HTML 将无效并且您的脚本将无法正常工作。

回答by Marko Dumic

Judging from your coding style I'd say you used the same ID for other elements(anchors) too. You should never put more than one ID value on the same page.

从您的编码风格来看,我会说您对其他元素(锚点)也使用了相同的 ID。您永远不应在同一页面上放置多个 ID 值。