简体   繁体   English

如何禁用网页上的所有href? 可以用JavaScript完成吗?

[英]How to disable all href on a web page? Can it be done with javascript?

I am working on a system which will force member to fill in their detail first then only can view other page for the website. 我正在使用一个系统,该系统将强制成员首先填写其详细信息,然后只能查看该网站的其他页面。 But I have no idea how to disable all the href link and user can only press on submit button to proceed. 但我不知道如何禁用所有href链接,用户只能按“提交”按钮才能继续。 I couldn't found how to disable all at 1 time and I only found to be disable href 1 by 1 by id with the condition they need to click on one of the submit button. 我找不到如何一次全部禁用的方法,我只发现按ID逐个禁用href 1,条件是他们需要单击一个提交按钮。 But what I want is it will disable when user is on the particular page and database status = 1 meanwhile href will be enable while status = 2 . 但是我想要的是,当用户在特定页面上并且数据库status = 1时,它将禁用,而status = 2时,href将被启用。

This is my submit button in profile-page-update.html : 这是profile-page-update.html我的提交按钮:

 <input type="submit" name="submit" onclick="xajax_editprofile(xajax.getFormValues('updateuser')); return false;" style="width:1px; visibility:hidden;">
<a href="#" onclick="xajax_editprofile(xajax.getFormValues('updateuser')); return false;">Save</a></div>

This is all the link I need to disable : 这就是我需要禁用的所有链接:

    <div class="nav">
        <ul>
            <li style="margin-left:20px"><a href="index.php">HOME</a></li>
            <li><a href="news-listing.php">NEWS</a></li>
            <li><a>EVENTS</a>
                <ul>
                    <li><a href="event-listing.php?id=1">UPCOMING EVENTS</a></li>
                    <li><a href="event-listing.php?id=2">PAST EVENTS</a></li>
                </ul>
            </li>
            <{if !$login}>
            <li><a class="open-popup-link" href="#test-popup">COMMUNITY</a></li>
            <{else}>
            <li><a href="community-listing.php">COMMUNITY</a></li>
            <{/if}>
            <li><a href="alumni-listing.php">ALUMNI LISTING</a></li>
        </ul>
    </div>

This is some code I found on internet: 这是我在互联网上找到的一些代码:

<input type="button" value="Open Drop Down" onclick="disableLinks()" />

function disableLinks() {

var links = document.getElementsByTagName('a');

for (var i = 0; i < links.length; i++) {

var link = links[i];

link.removeAttribute('href');

}

Technically speaking, you may disable the links by walking through all <a> elements using getElementsByName without jQuery or $("tagName") with jQuery. 从技术上讲,您可以通过使用不带jQuery的getElementsByName或jQuery的$("tagName")遍历所有<a>元素来禁用链接。

But that's not what you need, I think. 我想这不是您所需要的。

If your intention is: 如果您的意图是:

  • To hide links in order to prevent the user from going anywhere else than to the form target, then just create a page with only the form, its submit button, and nothing else, and especially no links. 要隐藏链接以防止用户除到达表单目标之外的其他任何地方,只需创建一个仅包含表单,其提交按钮,没有其他内容(尤其是没有链接)的页面。

    If you simply play with page's links through JavaScript, anyone may view the source of the page and recover the links, in order to bypass the form and to navigate to the specific pages. 如果您只是通过JavaScript播放页面的链接,那么任何人都可以查看页面的源并恢复链接,从而绕过表单并导航到特定页面。

  • To prevent the user to access anything else on the website (or to access some restricted resources), then use security checks server-side, with automated redirection if the user is not authorized to access a resource. 为了防止用户访问网站上的其他任何内容(或访问某些受限制的资源),然后在服务器端使用安全性检查,如果用户无权访问资源,则使用自动重定向。 If you're using a framework, very probably the functionality is already implemented and used to make the difference between authenticated users and guests: authenticated users will be able to access specific resources, while guests will be redirected by the framework to the log-in page. 如果您使用的是框架,则很有可能已经实现了该功能,并已用于区分经过身份验证的用户和访客:已认证的用户将能够访问特定资源,而访客将被框架重定向到登录页。

    If you simply hide the links on the page, but don't do any security checks server-side on the "protected" pages, anyone who already used the website before (or knows somebody who did) will know the exact links to enter, bypassing again the form. 如果您只是隐藏页面上的链接,但不对“受保护”页面上的服务器端进行任何安全检查,那么以前使用过该网站(或知道这样做的人)的任何人都将知道要输入的确切链接,再次绕过表格。

Using jQuery this will disable all <a> after page load. 使用jQuery这将在页面加载后禁用所有<a>

$(function(){

   $("a").on("click", function(){return false;});

})

And you just have to run below to enable them again 您只需要在下面运行即可再次启用它们

 $("a").off("click");

Demo 演示版

You could use jQuery to prevent the default event listener for the click event while you want the links to be disabled, then unbind it when the user is allowed to navigate again. 当您要禁用链接时,可以使用jQuery来防止click事件的默认事件侦听器,然后在允许用户再次导航时取消绑定。

//remove click ability
$("a").click(function(e){e.preventDefault()});
//reset
$("a").unbind('click');

Though this would need some other work if you have some click listeners you want to keep active on certain links etc. 尽管如果您需要一些单击侦听器,但要在某些链接等上保持活动状态,这可能需要做一些其他工作。

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

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