admin管理员组

文章数量:1183702

On Chromium 7.0.517.44 (64615) Ubuntu 10.10, I seem to be unable to change the type attribute of a <button> element:

> blah = document.createElement("button")
  <button>​</button>​
> blah.type
  "submit"
> blah.type = "button"
  "button"
> blah.type
  "submit"

Help?


On Firefox 3.6.12 and Opera 10.63, it works fine:

>>> blah = document.createElement("button")
    <button>
>>> blah.type
    "submit"
>>> blah.type = "button"
    "button"
>>> blah.type
    "button"

On Chromium 7.0.517.44 (64615) Ubuntu 10.10, I seem to be unable to change the type attribute of a <button> element:

> blah = document.createElement("button")
  <button>​</button>​
> blah.type
  "submit"
> blah.type = "button"
  "button"
> blah.type
  "submit"

Help?


On Firefox 3.6.12 and Opera 10.63, it works fine:

>>> blah = document.createElement("button")
    <button>
>>> blah.type
    "submit"
>>> blah.type = "button"
    "button"
>>> blah.type
    "button"
Share Improve this question asked Nov 13, 2010 at 2:07 erjiangerjiang 45.6k10 gold badges67 silver badges105 bronze badges 2
  • 5 I don't believe you SHOULD be able to. Firefox and Opera let you, Chrome doesn't, but I think IE will implode. I know it does for <input/> so I don't think <button> will be any different. – Robert Commented Nov 13, 2010 at 2:09
  • @Robert: You should be able to set the type attribute when dynamically creating new elements with script (createElement) - as appears to be the case above. Setting the type property has been fixed in Chrome and IE9, but IE8 and earlier 'implodes'. setAttribute resolves this (as stated in the accepted answer). – MrWhite Commented Feb 14, 2013 at 11:48
Add a comment  | 

3 Answers 3

Reset to default 24

Use setAttribute.

blah.setAttribute('type', 'button');

Change the type attribute on any of the input family generally doesn't work (I know for a fact it does not work on input).

What you may want to do is clone the element, replace the type attribute whilst the HTML is serialised and then append it after. Then delete the original.

======================================================================

javascript:

  function submit_button(){

      var btn=document.getElementById(button_id');
      btn.setAttribute('type', 'submit');

  }

======================================================================

Jquery:

 function submit_button(){

 $('#' + button_id).prop('type', 'submit');

   }

======================================================================

here it works.....

本文标签: htmlCan39t change ltbuttongt39s quottypequot with JavascriptStack Overflow