简体   繁体   English

单击时如何禁用链接?

[英]how to disable link when its clicked?

i want to disable link when i clicked on a link,here is my code: 我想在单击链接时禁用链接,这是我的代码:

<a href="?cmd=7" style="color:#00F; margin-left:15px; text-decoration:underline">Past 7 Days</a>
<a href="?cmd=14" style="color:#00F; margin-left:15px; text-decoration:underline">Past 14 Days</a>
<a href="?cmd=30" style="color:#00F; margin-left:15px; text-decoration:underline">Past 30 Days</a>
<a href="?cmd=custom" style="color:#00F; margin-left:15px; text-decoration:underline">Set A Custom Date Range</a>

i want when i click past 7 days link this link is disabled or ther links enabled,as soon then if i clicked on past 14 days link,past 7 day link is enabled and past 14 days link is disabled.how i do this? 我想要当我单击过去7天链接时禁用此链接或启用该链接,然后如果我单击过去14天链接,则启用过去7天链接而禁用过去14天链接。我该怎么做?

$('a').on('click',function(){
   $('a').removeAttr('disabled');
   $(this).attr('disabled',true);
})

or 要么

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

$('a').on('click',function(){
    $('a').removeClass('disable');
    $(this).addClass('disable');
})

CSS: CSS:

a:visited
{
 pointer-events: none;
 cursor: default;
}

use this code to disable using jquery 使用此代码禁用使用jquery

$("a").on('click',function(){
$(this).attr('disabled',true);
})

Try this: 尝试这个:

$('a').on("click", function (e) {
    e.preventDefault();
});

Since my reputation doesn't let me comment yet, I'll post is as an answer here. 由于我的声誉尚未允许我发表评论,因此我将在此处发布作为答案。 This is intended to improove Karthick Kumar Ganesh's answer. 这是为了改善Karthick Kumar Ganesh的答案。

Depending on what jq-version you are using, you should use .prop() instead of .attr() for setting attributes / propperties. 根据您使用的jq版本,您应该使用.prop()而不是.attr()来设置属性/属性。

Also I would use a class on your links if you're not just addressing anchors or adding get parameters, that should be disabled so that not all links are disabled after clicking. 另外,如果您不仅要处理锚点或添加get参数,我还将在您的链接上使用一个类,该类应被禁用,以便在单击后不会禁用所有链接。

$("a.disabable").on('click',function(){
  $(this).prop('disabled',true);
})

Lastly it makes perfect sense to add a target="_blank" or something similar to your links, so that your link opens in a new tab. 最后,添加target="_blank"或与您的链接类似的东西是很有意义的,这样您的链接就会在新标签页中打开。 Otherwise it makes no sense to me to disable a link, after it has been clicked. 否则,单击链接后禁用链接对我来说是没有意义的。

An example link would look something like this 一个示例链接看起来像这样

<a href="wher/you/want/to/go" class="disabable" target="blank" style="[...]">Foo</a>

This is the only solution so far that works across page loads as the OP described (I assume clicks on the anchors change the document location and therefore reload the page). 到目前为止,这是唯一一种可以在OP中描述的跨页面加载的解决方案(我认为单击锚点会更改文档位置,从而重新加载页面)。

HTML (added class attributes): HTML(添加的class属性):

<a class="cmd-7" href="?cmd=7" style="color:#00F; margin-left:15px; text-decoration:underline">Past 7 Days</a>
<a class="cmd-14" href="?cmd=14" style="color:#00F; margin-left:15px; text-decoration:underline">Past 14 Days</a>
<a class="cmd-30" href="?cmd=30" style="color:#00F; margin-left:15px; text-decoration:underline">Past 30 Days</a>
<a class="cmd-custom" href="?cmd=custom" style="color:#00F; margin-left:15px; text-decoration:underline">Set A Custom Date Range</a>

JavaScript (using the code from this answer ): JavaScript(使用此答案中的代码):

$(function() {
  // Get the cmd query parameter
  var cmd = getParameterByName('cmd');
  if(cmd) {
    // Disable the link
    $('a.cmd-' + cmd).click(function(event) {
      event.preventDefault();
    })
    // Add a class to allow styling
    .addClass('disabled');
  }
});
.disabled {
text-decoration:none;
color:black;
 }


<a class="links" href="##"> Link1 </a></br>
<a class="links" href="##"> Link2 </a></br>
<a class="links" href="##"> link3 </a></br>


$(function(){
   $(".links").click(function(){
    if($(this).hasClass("disabled")){
         return false;
    }
    else{
        $(".links").removeClass("disabled");
        $(this).addClass("disabled");
    }
});
})

I think It will work 我认为它将起作用

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

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