锚标记禁用

时间:2014-12-25 18:45:41

标签: jquery

我必须根据索引启用和禁用锚点,例如:当索引为3或更多时,应禁用锚点。 我编写了以下代码snipet但是当索引为3时锚没有被禁用。

if(index == 3){
     $("#anchor2").click(function(e){
                e.preventDefault(); 
                return false;
        });
 }

5 个答案:

答案 0 :(得分:2)

您只需将方案修改为:



    index=3;//change the value andcheck the result
    $('#anchor2').click(function (e) {
        if (index == 3) {
            e.preventDefault();
            return false;
        } else {
            alert('I am Enabled Now');
        }
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a id="anchor2" href="#">Click me!</a>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

$('#anchor2').click(function (e) {
    if (index >= 3) {
        e.preventDefault();
    }
    //if index<3 follow the link normally
});

答案 2 :(得分:1)

尝试这个技巧:

<a href="#" data-disabled >Click Here! disabled</a>

$('a[data-disabled]').click(function (e) {
    e.preventDefault();
    console.log('disabled');
});

只需在第3个锚标记上添加data-disabled属性。

<强> jsfiddle Demo

答案 3 :(得分:1)

你可以做这个css技巧:

if(index == 3){
    $("#anchor2").css('pointer-events', 'none');
}

答案 4 :(得分:0)

试试这个:

<a href='javascript:void(0);'>some text</a>