Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

javascript - 如何使用JavaScript禁用HTML按钮?(How to disable HTML button using JavaScript?)

I've read that you can disable (make physically unclickable) an HTML button simply by appending disable to its tag, but not as an attribute, as follows:

(我已经读过你可以通过对其标签添加disable而不是作为属性来禁用(使物理上不可点击)HTML按钮,如下所示:)

<input type="button" name=myButton value="disable" disabled>

Since this setting is not an attribute, how can I add this in dynamically via JavaScript to disable a button that was previously enabled?

(由于此设置不是属性,如何通过JavaScript动态添加此设置以禁用先前启用的按钮?)

  ask by Jack Roscoe translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since this setting is not an attribute

(由于此设置不是属性)

It is an attribute.

(这是一个属性。)

Some attributes are defined as boolean, which means you can specify their value and leave everything else out.

(某些属性定义为boolean,这意味着您可以指定其值并将其他所有内容保留。)

ie Instead of disabled=" (disabled) ", you include only the bold part.

(即,仅包括粗体部分而不是禁用=“ (禁用)

”。) In HTML 4, you should include only the bold part as the full version is marked as a feature with limited support (although that is less true now then when the spec was written).

(在HTML 4中,您应该只包含粗体部分,因为完整版本被标记为支持有限的功能 (尽管现在编写规范时不太正确)。)

As of HTML 5, the rules have changed and now you include only the name and not the value.

(从HTML 5开始,规则已经更改,现在只包含名称而不包含值。)

This makes no practical difference because the name and the value are the same.

(这没有实际区别,因为名称和值是相同的。)

The DOM property is also called disabled and is a boolean that takes true or false .

(DOM属性也称为disabled ,是一个带truefalse的布尔值。)

foo.disabled = true;

In theory you can also foo.setAttribute('disabled', 'disabled');

(理论上你也可以foo.setAttribute('disabled', 'disabled');)

and foo.removeAttribute("disabled") , but I wouldn't trust this with older versions of Internet Explorer (which are notoriously buggy when it comes to setAttribute ).

(和foo.removeAttribute("disabled") ,但我不相信旧版本的Internet Explorer(在setAttribute是众所周知的错误)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...