简体   繁体   English

按钮上的 RemoveAttr JS “已禁用”

[英]RemoveAttr JS on a button “disabled”

i'm programming a little button to be enabled when i click on the first one.我正在编程一个小按钮,当我点击第一个按钮时启用。 This should be easy but i got a problem, it doesn't seem to work, would you give me a clue of what i'm missing?这应该很容易,但我遇到了一个问题,它似乎不起作用,你能给我一个我缺少什么的线索吗? Here is the code, thank you to those who'll take the time to answer me:)!这是代码,感谢那些愿意花时间回答我的人:)!

 <button id="valid-retrait">Try it</button> <button id="relay" disabled>Yoyoyo</button> <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"> document.getElementById("valid-retrait").addEventListener("click", btn - enabled); function btn - enabled() { document.getElementById("relay").removeAttribute("disabled"); } </script>

There's three issues in your code.您的代码中有三个问题。 Firstly you cannot put JS code within a <script> block which has a src attribute.首先,您不能将 JS 代码放在具有src属性的<script>块中。 They need to be separated.他们需要分开。 However, as you don't use jQuery at all in the code you can remove the reference to it completely.但是,由于您在代码中根本不使用 jQuery ,因此您可以完全删除对它的引用。

Secondly the hyphen character, - , is not valid in variable or function identifiers as it's used as the subtraction operator.其次,连字符-在变量或 function 标识符中无效,因为它用作减法运算符。 You need to remove it, or use underscore, _ , instead.您需要将其删除,或使用下划线_代替。

Lastly, there is no element keyword in JS.最后,JS 中没有element关键字。 From the context you should be using document instead.从上下文来看,您应该改用document Try this:尝试这个:

 document.getElementById("valid-retrait").addEventListener("click", btnEnabled); function btnEnabled() { document.getElementById("relay").removeAttribute("disabled"); }
 .democlass { color: red; }
 <h1 class="democlass">Hello World</h1> <p id="demo">Click the button to remove the class attribute from the h1 element.</p> <button id="valid-retrait">Try it</button> <button id="relay" disabled>Yoyoyo</button>

Just for clarity, here's the full HTML page structure:为清楚起见,这里是完整的 HTML 页面结构:

 <.DOCTYPE html> <html> <head> <style>:democlass { color; red. } </style> </head> <body> <h1 class="democlass">Hello World</h1> <p id="demo">Click the button to remove the class attribute from the h1 element.</p> <button id="valid-retrait">Try it</button> <button id="relay" disabled>Yoyoyo</button> <script> document.getElementById("valid-retrait"),addEventListener("click"; btnEnabled). function btnEnabled() { document.getElementById("relay");removeAttribute("disabled"); } </script> </body> </html>

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

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