Html- 如何禁用 <a href>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28318057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:27:37  来源:igfitidea点击:

Html- how to disable <a href>?

htmlhref

提问by sarit rotshild

I created a button that open a modal window on click.

我创建了一个按钮,单击时打开一个模态窗口。

<a href="#"  data-toggle="modal" data-target="#myModal" class="signup-button gray-btn pl-pr-36" id="connectBtn"  data-role="disabled">Connect</a>

For some reason the data-role="disabled"doesn't work good. How can I disable it?

出于某种原因,data-role="disabled"效果不佳。我怎样才能禁用它?

回答by iivannov

You can use CSS to accomplish this:

您可以使用 CSS 来完成此操作:

<style>
    .disabled {
        pointer-events: none;
        cursor: default;
    }
</style>

<a href="somelink.html" class="disabled">Some link</a>

Example:http://jsfiddle.net/7EQJp/

示例:http : //jsfiddle.net/7EQJp/

Or you can use JavaScript to prevent the default action like this:

或者您可以使用 JavaScript 来阻止这样的默认操作:

$('.disabled').click(function(e){
    e.preventDefault();
})

回答by James Donnelly

I created a button...

我创建了一个按钮...

This is where you've gone wrong. You haven'tcreated a button, you've created an anchor element. If you had used a buttonelement instead, you wouldn't have this problem:

这就是你出错的地方。您还没有创建按钮,而是创建了一个锚元素。如果您button改为使用元素,则不会出现此问题:

<button type="button" data-toggle="modal" data-target="#myModal" data-role="disabled">
    Connect
</button>

If you are going to continue using an aelement instead, at the very least you should give it a roleattribute set to "button"and drop the hrefattribute altogether:

如果你打算继续使用一个a元素,至少你应该给它一个role属性设置为"button"href完全删除该属性:

<a role="button" ...>

Once you've done that you can introduce a piece of JavaScript which calls event.preventDefault()- here with eventbeing your click event.

完成后,您可以引入一段 JavaScript 调用event.preventDefault()- 这里event是您的点击事件。

回答by Grant Hood

.disabledLink.disabled {pointer-events:none;}

That should do it hope I helped!

应该这样做希望我有所帮助!

回答by Sameer Shaikh

<script>
    $(document).ready(function(){
        $('#connectBtn').click(function(e){
            e.preventDefault();
        })
    });
</script>

This will prevent the default action.

这将阻止默认操作。