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

我有一个锚链接,我想禁用一旦用户点击它。或者,删除文本周围的锚标记,但一定要保留文本。

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

我可以做到这一点很容易与一个按钮添加 .attr("disabled", "disabled");
我成功地添加了禁用属性,但是链接仍然可以单击。
我不在乎文字是否有下划线。

有线索吗?

当你点击错误的音乐家,它应该只是添加“错误”,然后成为无法点击。
当你点击,你是正确的,它应该添加“太棒了”,然后禁用所有 <a>标签。

259183 次浏览

You could use the onclick event to disable the click action:

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

Or you could just use something other than an <a> tag.

Just remove the href attribute from the anchor tag.

$('#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.

$('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;
});

Try this:

$('a').contents().unwrap();

The cleanest method would be to add a class with pointer-events:none when you want to disable a click. It would function like a normal label.

.disableClick{
pointer-events: none;
}

Add a css class:

.disable_a_href{
pointer-events: none;
}

Add this jquery:

$("#ThisLink").addClass("disable_a_href");
<a href='javascript:void(0);'>some text</a>

The best way is to prevent the default action. In the case of anchor tag, the default behavior is redirecting to href specified address.

So following javascript works best in the situation:

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

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.

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.

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

Use pointer-events CSS style. (as Jason MacDonald suggested)

See 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:

a[disabled], a[disabled]:hover {
pointer-events: none;
color: #e1e1e1;
}

Simply in SASS:

.some_class{
// styles...


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

Bootstrap provide us with .disabled class. Please use it.

But .disabled class only works when the 'a' tag already has class 'btn'. It doesn' t work on any old 'a' tag. The btn class may not be appropriate in some context as it has style connotations. 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. (Thank you, Les Nightingill for this advice).

The easyest way

In your html:

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

In your js:

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

If you use it as attribute works perfectly

This is the method I used to disable.Hope it helps.

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

Write this a single line of jQuery Code

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

if you want to write in css file

.hyperlink{
pointer-events: none;
}

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.

On the javascript side, the safest thing you can do is completely replace the <a> link with another tag, preserving the content:

/** 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.

In your case you must skip the href attribute.