Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
禁用<;a href>;在运行时使用Javascript_Javascript_Jquery_Html_Twitter Bootstrap - Fatal编程技术网

禁用<;a href>;在运行时使用Javascript

禁用<;a href>;在运行时使用Javascript,javascript,jquery,html,twitter-bootstrap,Javascript,Jquery,Html,Twitter Bootstrap,使用引导 <ul class="nav nav-pills nav-stacked col-sm-2 hidden" id="menu"> <li role="presentation" id="LiNewsFeed"><a href="javascript:GetNewsFeed();">News Feed</a></li> <li role="presenta

使用引导

  <ul class="nav nav-pills nav-stacked col-sm-2 hidden" id="menu">
                <li role="presentation" id="LiNewsFeed"><a href="javascript:GetNewsFeed();">News Feed</a></li>
                <li role="presentation" id="LiStatusUpdate"><a href="javascript:StatusUpdate();">Update Status</a></li>
                <li role="presentation" id="LiWriteWall"><a href="javascript:WriteOnWall();">Post On Wall</a></li> 
                <li role="presentation" id="LiNotifications"><a href="javascript:GetNotifications();">Notifications</a></li>
                <li role="presentation" id="LiLogOut"><a href="javascript:LogOut();">Logout</a></li> 
  </ul>
列表中的项目实际上看起来是禁用的,当我单击它时,它实际上调用javascript函数,因此,我需要的是禁用
,而不仅仅是
  • 我尝试在
    $(文档)之后添加此内容。准备好了吗

    $(".nav li.disabled a").click(function () {
                    return false;
    });
    
    但它实际上什么也没做

    我需要的是在我的Js代码中禁用
  • 后直接禁用
    ,而不是依赖于单击事件

    似乎没有办法禁用
    ,所以我需要一种解决方法


    任何帮助都将不胜感激。

    如果家长有禁用的类,我会检查每个链接单击

    $('.nav li a').click(function () {
        if($(this).parent('li').hasClass('disabled')) {
            return false;
        }
        return true;
    });
    
    编辑,根据OP提供的更多信息,我建议如下:

     $('#LiNewsFeed').addClass('disabled');
    
    $('.nav li a').each(function() {
        var $this = $(this);
    
        // store reference of 'href' attr in case link is re-enabled
        $this.data('href', $this.attr('href'));
    
        if ($this.parent('li').hasClass('disabled')) {
            // remove href attribute disabling click
            $this.removeAttr('href');
        } else {
            // restore href
            $this.attr('href', this.data('href'));
        }
    });
    
    var events = {
        '#LiNewsFeed': 'GetNewsFeed',
        '#LiStatusUpdate': 'StatusUpdate'
        '#LiWriteWall': 'WriteOnWall',
        '#LiNotifications': 'GetNotifications',
        '#LiLogOut': 'LogOut'
    };
    
    for (var selector in events) {
        if (events.hasOwnProperty(selector)) {
            try {
                $(selector).click(function () {
                    // assuming function is global
                    if (typeof window[events[selector]] === 'function') {
                        // call function
                        window[events[selector]]();
                    }
                    // this is needed if the a element still has a href attr
                    return false;
                });
            } catch (e) {
                console.log('Invalid Selector');
            }
        }
    }
    
    应在添加/删除li元素上禁用的类后运行此代码

    编辑2-您可以执行以下操作,而不是从
    链接的href调用函数:

     $('#LiNewsFeed').addClass('disabled');
    
    $('.nav li a').each(function() {
        var $this = $(this);
    
        // store reference of 'href' attr in case link is re-enabled
        $this.data('href', $this.attr('href'));
    
        if ($this.parent('li').hasClass('disabled')) {
            // remove href attribute disabling click
            $this.removeAttr('href');
        } else {
            // restore href
            $this.attr('href', this.data('href'));
        }
    });
    
    var events = {
        '#LiNewsFeed': 'GetNewsFeed',
        '#LiStatusUpdate': 'StatusUpdate'
        '#LiWriteWall': 'WriteOnWall',
        '#LiNotifications': 'GetNotifications',
        '#LiLogOut': 'LogOut'
    };
    
    for (var selector in events) {
        if (events.hasOwnProperty(selector)) {
            try {
                $(selector).click(function () {
                    // assuming function is global
                    if (typeof window[events[selector]] === 'function') {
                        // call function
                        window[events[selector]]();
                    }
                    // this is needed if the a element still has a href attr
                    return false;
                });
            } catch (e) {
                console.log('Invalid Selector');
            }
        }
    }
    
    通过这种方式,您可以控制函数的调用,并检查是否应该在不改变元素的情况下调用它,可能需要粘贴一个

    if (!$(this).parent('li').hasClass('disabled')) {
        ...
    } 
    

    关于函数调用。

    如果父类具有禁用的类,我会检查每个链接单击

    $('.nav li a').click(function () {
        if($(this).parent('li').hasClass('disabled')) {
            return false;
        }
        return true;
    });
    
    编辑,根据OP提供的更多信息,我建议如下:

     $('#LiNewsFeed').addClass('disabled');
    
    $('.nav li a').each(function() {
        var $this = $(this);
    
        // store reference of 'href' attr in case link is re-enabled
        $this.data('href', $this.attr('href'));
    
        if ($this.parent('li').hasClass('disabled')) {
            // remove href attribute disabling click
            $this.removeAttr('href');
        } else {
            // restore href
            $this.attr('href', this.data('href'));
        }
    });
    
    var events = {
        '#LiNewsFeed': 'GetNewsFeed',
        '#LiStatusUpdate': 'StatusUpdate'
        '#LiWriteWall': 'WriteOnWall',
        '#LiNotifications': 'GetNotifications',
        '#LiLogOut': 'LogOut'
    };
    
    for (var selector in events) {
        if (events.hasOwnProperty(selector)) {
            try {
                $(selector).click(function () {
                    // assuming function is global
                    if (typeof window[events[selector]] === 'function') {
                        // call function
                        window[events[selector]]();
                    }
                    // this is needed if the a element still has a href attr
                    return false;
                });
            } catch (e) {
                console.log('Invalid Selector');
            }
        }
    }
    
    应在添加/删除li元素上禁用的类后运行此代码

    编辑2-您可以执行以下操作,而不是从
    链接的href调用函数:

     $('#LiNewsFeed').addClass('disabled');
    
    $('.nav li a').each(function() {
        var $this = $(this);
    
        // store reference of 'href' attr in case link is re-enabled
        $this.data('href', $this.attr('href'));
    
        if ($this.parent('li').hasClass('disabled')) {
            // remove href attribute disabling click
            $this.removeAttr('href');
        } else {
            // restore href
            $this.attr('href', this.data('href'));
        }
    });
    
    var events = {
        '#LiNewsFeed': 'GetNewsFeed',
        '#LiStatusUpdate': 'StatusUpdate'
        '#LiWriteWall': 'WriteOnWall',
        '#LiNotifications': 'GetNotifications',
        '#LiLogOut': 'LogOut'
    };
    
    for (var selector in events) {
        if (events.hasOwnProperty(selector)) {
            try {
                $(selector).click(function () {
                    // assuming function is global
                    if (typeof window[events[selector]] === 'function') {
                        // call function
                        window[events[selector]]();
                    }
                    // this is needed if the a element still has a href attr
                    return false;
                });
            } catch (e) {
                console.log('Invalid Selector');
            }
        }
    }
    
    通过这种方式,您可以控制函数的调用,并检查是否应该在不改变元素的情况下调用它,可能需要粘贴一个

    if (!$(this).parent('li').hasClass('disabled')) {
        ...
    } 
    

    关于函数调用。

    如果父类具有禁用的类,我会检查每个链接单击

    $('.nav li a').click(function () {
        if($(this).parent('li').hasClass('disabled')) {
            return false;
        }
        return true;
    });
    
    编辑,根据OP提供的更多信息,我建议如下:

     $('#LiNewsFeed').addClass('disabled');
    
    $('.nav li a').each(function() {
        var $this = $(this);
    
        // store reference of 'href' attr in case link is re-enabled
        $this.data('href', $this.attr('href'));
    
        if ($this.parent('li').hasClass('disabled')) {
            // remove href attribute disabling click
            $this.removeAttr('href');
        } else {
            // restore href
            $this.attr('href', this.data('href'));
        }
    });
    
    var events = {
        '#LiNewsFeed': 'GetNewsFeed',
        '#LiStatusUpdate': 'StatusUpdate'
        '#LiWriteWall': 'WriteOnWall',
        '#LiNotifications': 'GetNotifications',
        '#LiLogOut': 'LogOut'
    };
    
    for (var selector in events) {
        if (events.hasOwnProperty(selector)) {
            try {
                $(selector).click(function () {
                    // assuming function is global
                    if (typeof window[events[selector]] === 'function') {
                        // call function
                        window[events[selector]]();
                    }
                    // this is needed if the a element still has a href attr
                    return false;
                });
            } catch (e) {
                console.log('Invalid Selector');
            }
        }
    }
    
    应在添加/删除li元素上禁用的类后运行此代码

    编辑2-您可以执行以下操作,而不是从
    链接的href调用函数:

     $('#LiNewsFeed').addClass('disabled');
    
    $('.nav li a').each(function() {
        var $this = $(this);
    
        // store reference of 'href' attr in case link is re-enabled
        $this.data('href', $this.attr('href'));
    
        if ($this.parent('li').hasClass('disabled')) {
            // remove href attribute disabling click
            $this.removeAttr('href');
        } else {
            // restore href
            $this.attr('href', this.data('href'));
        }
    });
    
    var events = {
        '#LiNewsFeed': 'GetNewsFeed',
        '#LiStatusUpdate': 'StatusUpdate'
        '#LiWriteWall': 'WriteOnWall',
        '#LiNotifications': 'GetNotifications',
        '#LiLogOut': 'LogOut'
    };
    
    for (var selector in events) {
        if (events.hasOwnProperty(selector)) {
            try {
                $(selector).click(function () {
                    // assuming function is global
                    if (typeof window[events[selector]] === 'function') {
                        // call function
                        window[events[selector]]();
                    }
                    // this is needed if the a element still has a href attr
                    return false;
                });
            } catch (e) {
                console.log('Invalid Selector');
            }
        }
    }
    
    通过这种方式,您可以控制函数的调用,并检查是否应该在不改变元素的情况下调用它,可能需要粘贴一个

    if (!$(this).parent('li').hasClass('disabled')) {
        ...
    } 
    

    关于函数调用。

    如果父类具有禁用的类,我会检查每个链接单击

    $('.nav li a').click(function () {
        if($(this).parent('li').hasClass('disabled')) {
            return false;
        }
        return true;
    });
    
    编辑,根据OP提供的更多信息,我建议如下:

     $('#LiNewsFeed').addClass('disabled');
    
    $('.nav li a').each(function() {
        var $this = $(this);
    
        // store reference of 'href' attr in case link is re-enabled
        $this.data('href', $this.attr('href'));
    
        if ($this.parent('li').hasClass('disabled')) {
            // remove href attribute disabling click
            $this.removeAttr('href');
        } else {
            // restore href
            $this.attr('href', this.data('href'));
        }
    });
    
    var events = {
        '#LiNewsFeed': 'GetNewsFeed',
        '#LiStatusUpdate': 'StatusUpdate'
        '#LiWriteWall': 'WriteOnWall',
        '#LiNotifications': 'GetNotifications',
        '#LiLogOut': 'LogOut'
    };
    
    for (var selector in events) {
        if (events.hasOwnProperty(selector)) {
            try {
                $(selector).click(function () {
                    // assuming function is global
                    if (typeof window[events[selector]] === 'function') {
                        // call function
                        window[events[selector]]();
                    }
                    // this is needed if the a element still has a href attr
                    return false;
                });
            } catch (e) {
                console.log('Invalid Selector');
            }
        }
    }
    
    应在添加/删除li元素上禁用的类后运行此代码

    编辑2-您可以执行以下操作,而不是从
    链接的href调用函数:

     $('#LiNewsFeed').addClass('disabled');
    
    $('.nav li a').each(function() {
        var $this = $(this);
    
        // store reference of 'href' attr in case link is re-enabled
        $this.data('href', $this.attr('href'));
    
        if ($this.parent('li').hasClass('disabled')) {
            // remove href attribute disabling click
            $this.removeAttr('href');
        } else {
            // restore href
            $this.attr('href', this.data('href'));
        }
    });
    
    var events = {
        '#LiNewsFeed': 'GetNewsFeed',
        '#LiStatusUpdate': 'StatusUpdate'
        '#LiWriteWall': 'WriteOnWall',
        '#LiNotifications': 'GetNotifications',
        '#LiLogOut': 'LogOut'
    };
    
    for (var selector in events) {
        if (events.hasOwnProperty(selector)) {
            try {
                $(selector).click(function () {
                    // assuming function is global
                    if (typeof window[events[selector]] === 'function') {
                        // call function
                        window[events[selector]]();
                    }
                    // this is needed if the a element still has a href attr
                    return false;
                });
            } catch (e) {
                console.log('Invalid Selector');
            }
        }
    }
    
    通过这种方式,您可以控制函数的调用,并检查是否应该在不改变元素的情况下调用它,可能需要粘贴一个

    if (!$(this).parent('li').hasClass('disabled')) {
        ...
    } 
    

    围绕函数调用。

    使用下面的代码。检查工作示例


    使用下面的代码。检查工作示例


    使用下面的代码。检查工作示例


    使用下面的代码。检查工作示例


    在javascript(运行时)中禁用
    LI
    ,应使用
    .on
    绑定禁用链接上的事件:

    $(".nav").on('click', 'li.disabled a', function () {
        return false;
    }); 
    

    在javascript(运行时)中禁用
    LI
    ,应使用
    .on
    绑定禁用链接上的事件:

    $(".nav").on('click', 'li.disabled a', function () {
        return false;
    }); 
    

    在javascript(运行时)中禁用
    LI
    ,应使用
    .on
    绑定禁用链接上的事件:

    $(".nav").on('click', 'li.disabled a', function () {
        return false;
    }); 
    

    在javascript(运行时)中禁用
    LI
    ,应使用
    .on
    绑定禁用链接上的事件:

    $(".nav").on('click', 'li.disabled a', function () {
        return false;
    }); 
    

    你能把a转换成span吗

    (代码未测试)

    $(.nav li.disabled a”).replaceWith(函数(){return”“+this.innerHTML+”“;});
    
    能否将a转换为span

    (代码未测试)

    $(.nav li.disabled a”).replaceWith(函数(){return”“+this.innerHTML+”“;});
    
    能否将a转换为span

    (代码未测试)

    $(.nav li.disabled a”).replaceWith(函数(){return”“+this.innerHTML+”“;});
    
    能否将a转换为span

    (代码未测试)

    $(.nav li.disabled a”).replaceWith(函数(){return”“+this.innerHTML+”“;});
    
    使用$(“.nav li.disabled a”)。单击(函数(事件){event.preventDefault();})@jQuery谢谢,但我想禁用a对象,而不是依赖于click事件,因为它与其他事件混合在一起,不能正确调用它。然后可以取消绑定click事件$(“.nav li.disabled a”)。取消绑定('click')或在jQuery$(“.nav li.disabled a”)的新版本中。关闭('click'))检查我的答案可能会有帮助you@HelpASisterOut您尝试过我的解决方案吗?使用$(“.nav li.disabled a”)。单击(函数(事件){event.preventDefault();})@jQuery谢谢,但我想禁用a对象,而不是依赖于click事件,因为它与其他事件混合在一起,不能正确调用它。然后可以取消绑定click事件$(“.nav li.disabled a”)。取消绑定('click')或在jQuery$(“.nav li.disabled a”)的新版本中。关闭('click'))检查我的答案可能会有帮助you@HelpASisterOut您尝试过我的解决方案吗?使用$(“.nav li.disabled a”)。单击(函数(事件){event.preventDefault();})@jQuery谢谢,但我想禁用a对象,而不是依赖于click事件,因为它与其他事件混合在一起,不能正确调用它。然后可以取消绑定click事件$(“.nav li.disabled a”)。取消绑定('click')或在jQuery$(“.nav li.disabled a”)的新版本中。关闭('click'))检查我的答案可能会有帮助you@HelpASisterOut您尝试过我的解决方案吗?使用$(“.nav li.disabled a”)。单击(函数(事件){event.preventDefault();})@jQuery谢谢,但我想禁用a对象,而不是依赖于click事件,因为它与其他事件混合在一起,不能正确调用它。然后可以取消绑定click event$(“.nav li.disabled a”)。取消绑定('click')或在新版本的jQuery中