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
|
3 Answers
Reset to default 24Use 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
版权声明:本文标题:html - Can't change <button>'s "type" with Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738297508a2073454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<input/>
so I don't think<button>
will be any different. – Robert Commented Nov 13, 2010 at 2:09type
attribute when dynamically creating new elements with script (createElement
) - as appears to be the case above. Setting thetype
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