简体   繁体   English

如何在 javascript 中单击按钮时删除 css 样式?

[英]How to remove css style on button click in javascript?

I have created an form in which I have made the input field disabled by default->我创建了一个表单,在该表单中,默认情况下禁用了输入字段->

<input class="name"  type="text" maxlength="50" value="Hritika Agarwal" disabled="" />

I want to make this editable on button click.我想在按钮单击时使其可编辑。 So created a button like this ->所以创建了一个这样的按钮->

 <button class="accept-btn" onclick="myFunction()">Edit</button>


   function myFunction() {
   document.getElementById("name").removeAttribute("disabled");
   }

But It's not working.但它不起作用。

I have even tried like this also->我什至也试过这样->

     function myFunction() {
    document.getElementById("name").style.disabled = "false";
    }

But again nothing happened.但又什么也没发生。 How could I resolve this>我该如何解决这个问题>

You use getElementById to find an element with the id name but there is not such thing.您使用getElementById查找具有 id name的元素,但没有这样的东西。 Use <input id="name" instead of class, then the first will work.使用<input id="name"而不是 class,那么第一个就可以了。

The second can not work, since disabled is not a style-property.第二个不起作用,因为disabled不是样式属性。

 function myFunction() {
   document.getElementsByClassName("name")[0].removeAttribute("disabled");
 }

Or或者

<input class="name" id="name"  type="text" maxlength="50" value="Hritika Agarwal" disabled="" />

if you want to select elemnt by class name use getElementsByClassName .如果你想 select 元素 class 名称使用getElementsByClassName

document.getElementsByClassName("name")[0].disabled = false;

If如果

select elemnt by id use getElementById .change input to select elemnt by id 使用getElementById .change 输入

<input id="name"  type="text" maxlength="50" value="Hritika Agarwal" disabled="" />

then然后

 document.getElementById("name").disabled = false;

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

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