从html输入中删除size属性

时间:2014-12-16 19:07:16

标签: javascript jquery html

我有以下HTML代码:

<input id="user_current_password" name="user[current_password]" size="20" type="password" style="display: none;">

我想删除size属性。我已经尝试过这样的事情:

$("#user_current_password").removeAttr("size")
$("#user_current_password").attr("size","auto")
$("#user_current_password").attr("size","")

但它总是给我错误:DOMException:无法在'HTMLInputElement'上设置'size'属性:提供的值是0,这是一个无效的大小。

由于

1 个答案:

答案 0 :(得分:2)

这应解决它,仅使用javascript:

var a = document.getElementById('user_current_password');
a.setAttribute('size','auto'); // <<< set size as wished

或者,删除它:

a.removeAttribute('size');

请注意将此代码放在

window.onload = function() { /* code */ }

因为必须加载DOM才能让代码找到Elements