2014-01-30 110 views
0

我想禁用链接时,我一个链接上点击,这里是我的代码:如何禁用链接时,其点击?

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

我希望当我点击过去7天链接此链接激活,尽快那么如果我点击禁用或疗法链接在过去14天的链接中,过去7天的链接已启用,过去14天的链接已停用。请问我这样做?

+0

我是唯一一个在这里谁认为你想实现页面加载效果? –

+0

没有人回答帮助我 –

回答

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

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

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

我认为OP希望在页面加载时达到效果。 –

+0

当我使用你的代码 –

+0

它给出错误TypeError:$(...)为空 –

0

试试这个:

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

由于我的名声不允许我评论,我会发布在这里作为答案。这是为了改进Karthick Kumar Ganesh的回答。

根据您使用的jq版本,您应该使用.prop()而不是.attr()来设置属性/属性。

另外我会在你的链接上使用一个类,如果你不只是寻址锚或添加获取参数,应该禁用,以便在点击后不会禁用所有链接。

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

最后它非常有意义添加一个类似于你的链接target="_blank"什么的,让你的链接在新标签中打开。否则,在点击链接后禁用链接是没有意义的。

一个例子链接看起来像这样

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

这是迄今为止解决方案,整个页面加载的作品所描述的OP(我假设在锚点击更改文件位置,因此重新加载页面)。

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(使用this answer代码):我认为它会工作

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

当我使用你的代码时,它给出了错误ReferenceError:getParameterByName未定义 –

+0

如何删除这个 –

+0

@Arif我写了“使用该代码[此答案](http://stackoverflow.com/questions/901115/how-can -i-得到的查询字符串值入的JavaScript/901144#901144)”。所以你应该包含在参考答案中声明的函数。 –

0
.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"); 
    } 
}); 
})