如何为元素< input>设置'size'属性?使用属性对象? [英] How can I set the 'size' attribute for the element <input> using an attribute object?
问题描述
我是一名jQuery初学者,并且遇到过在页面上创建新元素的方法之一。我能够很好地创建元素。但是,其中一个属性(大小)默认为20,即使我明确指出它应该更大。这是一个jQuery错误,还是我错过了什么?属性对象中的其他属性(类型,自动对焦)工作正常。
I am a jQuery beginner and have come across one of the methods to create a new element on the page. I am able to create the element fine. However, one of the attributes (size) defaults to 20 even though I clearly state that it should be bigger. Is this a jQuery bug, or am I missing something? The other attributes in the attribute object (type, autofocus) work fine.
我使用的代码是:
$('<input>', {
'type' : 'text',
'size' : 50,
'autofocus' : 'true'
});
我没有任何CSS文件。
I do not have any CSS file.
当我使用attr()方法时,我可以设置大小。但我想知道我不是唯一一个无法使用属性对象为元素设置size属性的人。我正在使用jQuery 1.9.1。
When I use the attr() method, I am able to set the size. But I want to know that I am not the only one who is unable to set the size attribute for the element using an attribute object. I am using jQuery 1.9.1.
任何答案都将不胜感激。
Any answers will be appreciated.
推荐答案
使用 .prop()
功能:
$('<input>', {
'type' : 'text',
'autofocus' : 'true'
}).prop('size','50');
或者这个语法:
$('<input>', {
'type': 'text',
prop: {
size: "50"
},
'autofocus': 'true'
})
jsFiddle example
这是发现它时的旧错误报告,并解释了为什么它们没有重新发布 - 启用旧功能。
Here's the old bug report when it was discovered and the explanation as to why they didn't re-enable the old functionality.
这篇关于如何为元素< input>设置'size'属性?使用属性对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!