简体   繁体   English

禁用锚标记的按钮

[英]Disabling the button of an anchor tag

I have a button inside the anchor tag(defined it using class). 我在锚标记(使用类定义它)内有一个按钮。

<a id="moreButton" class="contactButtonSmall" style="position:absolute; left:225px; top:165px;  FONT-WEIGHT:normal; FONT-SIZE:11pt;" onclick="doSomething();">More</a>

Now I want to disable it.So I have used the following code to disable the anchor tag. 现在我想禁用它,因此我使用以下代码禁用了定位标记。

moreButton.disabled = true;

The anchor tag is not working after disabling it , but the button of anchor still looks as if it is not disabled ie not grayed out. 锚点标签在禁用后不起作用,但是锚点按钮仍然看起来像未禁用,即未变灰。 Is there any way to disable the button? 有什么方法可以禁用该按钮吗? Please let me know if you need any additional information. 如果您需要任何其他信息,请告诉我。

As others have said, inline CSS is bad practice so you should export your style code to a separate CSS file, as so: 正如其他人所说,内联CSS是一种不好的做法,因此您应该将样式代码导出到单独的CSS文件中,如下所示:

.contactButtonSmall {
  position:absolute;
  left:225px;
  top:165px; 
  font-weight:normal;
  font-size:11pt;
}

Then you can use the :disabled selector to change the appearance of the button when it is disabled: 然后,可以使用:disabled选择器来更改:disabled按钮时的外观:

.contactButtonSmall:disabled {
  /* Styling for disabled button */
}

The best way to disable an anchor tag is to give it the correct pointer-events property. 禁用锚标记的最佳方法是为其提供正确的指针事件属性。 Here's a simple example how to disable the anchor tag with one simple CSS line: 这是一个简单的示例,说明如何使用一条简单的CSS行禁用锚标记:

 a { pointer-events: none; } 
 <a href="https://google.com">I am a disabled anchor tag</a> 

I have used button along with the style attributes 我已经使用了button以及样式属性

background-color: Transparent;
    border: none;

instead of anchor tag to fix the issue. 而不是锚标记来解决此问题。 The style attributes helped to remove the grayed out area of the original html button and to keep my own image for the button. 样式属性有助于删除原始html按钮的灰色区域,并为按钮保留自己的图像。

example code is given below: 示例代码如下:

<button> id="moreButton" class="contactButtonSmall" style="position:absolute; left:225px; top:165px;  FONT-WEIGHT:normal; FONT-SIZE:11pt; background-color: Transparent;border: none;" onclick="doSomething();">More</button>

In CSS file: 在CSS文件中:

.contactButtonSmall {
    position:relative;
    display: block; /* 'convert' <a> to <div> */
    width: 60px;
    max-height: 20px;
    padding-top: 2px;
    padding-bottom: 2px;
    background-position: center top;
    background-repeat: no-repeat;
    background-image: url(../contactImages/blankSmallButton.gif);
    text-decoration: none;
    text-align: center;
    cursor:pointer;
    background-color: Transparent;
    border: none;
}

You can use a mixture of CSS and JS to accomplish this: 您可以混合使用CSS和JS来完成此任务:

HTML: HTML:

<a href="/" id="myLink">
    click me!
</a>

CSS: CSS:

#myLink {
    background: red
}

a#myLink.disabledLink {
    background: grey;
    cursor: not-allowed;
}

JS: JS:

document.getElementById("myLink").onclick = function(e)
{
    e.preventDefault();
    this.className += " disabledLink";
}

jsfiddle here jsfiddle在这里

this on click prevents the default action of the anchor tag and assigns it a class. 单击该按钮可阻止锚标记的默认操作,并为其分配一个类。 The class has css that makes the cursor show the now-allowed icon as well as changing background colour to grey. 该类具有css,使光标显示now-allowed图标以及将背景颜色更改为灰色。

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

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