简体   繁体   English

如何使锚链接不可点击或禁用?

[英]How do you make an anchor link non-clickable or disabled?

I have an anchor link that I want to disable once the user clicks on it.我有一个锚链接,一旦用户单击它,我想禁用它。 Or, remove the anchor tag from around the text, but definitely keep the text.或者,从文本周围删除锚标记,但绝对保留文本。

<a href='' id='ThisLink'>some text</a>

I can do this easily with a button by adding .attr("disabled", "disabled");我可以通过添加.attr("disabled", "disabled");
I successfully added the disabled property, but the link was still clickable.我成功添加了 disabled 属性,但链接仍然可以点击。
I don't really care if the text is underlined or not.我真的不在乎文本是否带下划线。

Any clue?有什么线索吗?

When you click on the wrong musician, it should just add "Wrong" and then become unclickable.当您点击错误的音乐家时,它应该只添加“错误”,然后变得不可点击。
When you click and you are correct, it should add "Awesome" and then disable all <a> tags.当您单击并正确时,它应该添加“Awesome”,然后禁用所有<a>标记。

The cleanest method would be to add a class with pointer-events:none when you want to disable a click. 最干净的方法是在要禁用单击时添加一个带指针事件的类:none。 It would function like a normal label. 它的功能类似于普通标签。

.disableClick{
    pointer-events: none;
}
<a href='javascript:void(0);'>some text</a>

Use pointer-events CSS style. 使用指针事件 CSS样式。 (as Jason MacDonald suggested) (正如Jason MacDonald建议的那样)

See MDN https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events . 请参阅MDN https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events Its supported in most browsers. 大多数浏览器都支持它。

Simple adding "disabled" attribute to anchor will do the job if you have global CSS rule like following: 如果你有如下全局CSS规则,那么简单地将“disabled”属性添加到锚点就可以完成这项工作:

a[disabled], a[disabled]:hover {
   pointer-events: none;
   color: #e1e1e1;
}
$('a').removeAttr('href')

or 要么

$('a').click(function(){ return false})

It depends on situation 这取决于情况

I just realized what you were asking for(I hope). 我刚刚意识到你的要求(我希望)。 Here's an ugly solution 这是一个丑陋的解决方案

var preventClick = false;

$('#ThisLink').click(function(e) {
    $(this)
       .css('cursor', 'default')
       .css('text-decoration', 'none')

    if (!preventClick) {
        $(this).html($(this).html() + ' lalala');
    }

    preventClick = true;

    return false;
});

Add a css class: 添加一个css类:

.disable_a_href{
    pointer-events: none;
}

Add this jquery : 添加此jquery

$("#ThisLink").addClass("disable_a_href"); 

Bootstrap provide us with .disabled class. Bootstrap为我们提供.disabled类。 Please use it. 请使用它。

But .disabled class only works when the 'a' tag already has class 'btn'. 但是.disabled类仅在'a'标记已经具有类'btn'时才有效。 It doesn' t work on any old 'a' tag. 它不适用于任何旧的'a'标签。 The btn class may not be appropriate in some context as it has style connotations. btn类在某些上下文中可能不合适,因为它具有样式内涵。 Under the covers, the .disabled class sets pointer-events to none , so you can make CSS to do the same thing as Saroj Aryal and Vitrilo have sugested. 在封面下, .disabled类将pointer-eventsnone ,因此您可以使CSS与Saroj Aryal和Vitrilo进行相同的操作。 (Thank you, Les Nightingill for this advice). (谢谢你,Les Nightingill的建议)。

The best way is to prevent the default action. 最好的方法是阻止默认操作。 In the case of anchor tag, the default behavior is redirecting to href specified address. 对于锚标记,默认行为是重定向到href指定的地址。

So following javascript works best in the situation: 因此,遵循javascript在这种情况下效果最佳:

$('#ThisLink').click(function(e)
{
    e.preventDefault();
});

只需从锚标记中删除href属性即可。

Write this a single line of jQuery Code 写一行jQuery代码

$('.hyperlink').css('pointer-events','none');

if you want to write in css file 如果你想写css文件

.hyperlink{
    pointer-events: none;
}

Create following class in style sheet : 在样式表中创建以下类:

  .ThisLink{
           pointer-events: none;
           cursor: default;
    }

Add this class to you link dynamically as follow. 将此类添加到您动态链接,如下所示。

 <a href='' id='elemID'>some text</a>

    //   or using jquery
<script>
    $('#elemID').addClass('ThisLink');
 </script>

Jason MacDonald comments worked for me, tested in Chrome, Mozila and IE. Jason MacDonald评论为我工作,在Chrome,Mozila和IE中测试过。

Added gray color to show disable effect. 添加灰色以显示禁用效果。

.disable_a_href{
    pointer-events: none;
    **color:#c0c0c0 !important;**
}

Jquery was selecting only first element in the anchor list, added meta character (*) to select and disable all element with id #ThisLink . Jquery只选择了锚列表中的第一个元素,添加了元字符(*)来选择和禁用id为#ThisLink所有元素。

$("#ThisLink*").addClass("disable_a_href"); 

You could use the onclick event to disable the click action: 您可以使用onclick事件来禁用单击操作:

<a href='' id='ThisLink' onclick='return false'>some text</a>

Or you could just use something other than an <a> tag. 或者你可以只使用<a>标签以外的东西。

Simply in SASS: 只需在SASS:

.some_class{
     // styles...

     &.active {
       pointer-events:none;
     }
}

这是我用来禁用的方法。希望它有帮助。

$("#ThisLink").attr("href","javascript:;");

Never trust the browser because the user can change the page in any way without the server's knowledge.永远不要相信浏览器,因为用户可以在服务器不知情的情况下以任何方式更改页面。

If a link is to work only once, the first thing you need to do is make sure that server side the click is accepted only once (with an onetime token specified as querystring for example), because the URL present in the href attribute can be copied by the user and inserted in the navigation bar of the browser and runned multiple times.如果链接只能工作一次,您需要做的第一件事是确保服务器端的点击只被接受一次(例如,使用一次性令牌指定为查询字符串),因为 href 属性中存在的 URL 可以是由用户复制并插入浏览器的导航栏中并多次运行。

On the javascript side, the safest thing you can do is completely replace the <a> link with another tag, preserving the content:在 javascript 方面,您可以做的最安全的事情是用另一个标签完全替换<a>链接,保留内容:

/** Replace element, preserving attributes and moving descendant nodes from the previous one.
 *
 * @param {HTMLElement} element Element to be replaced changing tag.
 * @param {String} new_tag New element tag.
 * @return {HTMLElement} New created element.
 */
function rename_element_tag(element, new_tag) {
    let new_block = document.createElement(new_tag);
    for (let j = 0; j < element.attributes.length; ++j)
        new_block.setAttribute(element.attributes[j].name, element.attributes[j].value);

    $(new_block).insertAfter(element);
    while (element.childNodes.length > 0)
        new_block.appendChild(element.childNodes[0]);

    $(element).remove();

    return new_block;
}

This function replaces the passed element in place by "modifying" the tag, and preserves attributes and content by iterating all child nodes via vanilla javascript instead of jQuery to handle text nodes as well.这个 function 通过“修改”标签来替换传递的元素,并通过使用 vanilla javascript 而不是 jQuery 迭代所有子节点来保留属性和内容以处理文本节点。

In your case you must skip the href attribute.在您的情况下,您必须跳过href属性。

试试这个:

$('a').contents().unwrap();
$('#ThisLink').one('click',function(){
  $(this).bind('click',function(){
    return false;
  });
});

This would be another way to do this, the handler with return false , which will disable the link, will be added after one click. 这将是另一种方法,一次点击后将添加return false的处理程序,它将禁用链接。

The easyest way 最简单的方法

In your html: 在你的HTML中:

<a id="foo" disabled="true">xxxxx<a>

In your js: 在你的js中:

$('#foo').attr("disabled", false);

If you use it as attribute works perfectly 如果您使用它作为属性完美地工作

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

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