我知道我可以打电话:
document.getElementsByTagName("input");
Run Code Online (Sandbox Code Playgroud)
我是否需要遍历并检查type =="text"的属性?
我知道jQuery有input[type="text"].有没有简单的JavaScript方法来实现这一目标?
除非代码在支持的浏览器中querySelectorAll运行,否则大多数情况下都是正确的.这是跨浏览器代码:
var textInputs;
if (document.querySelectorAll)
{
textInputs = document.querySelectorAll('input[type=text]');
}
else
{
textInputs = [];
var unfiltered = document.getElementsByTagName("input"),
i = unfiltered.length,
input;
while(i--)
{
input = unfiltered[i];
if (!input.type || input.type === 'text')
{
textInputs.push(input);
}
}
}
Run Code Online (Sandbox Code Playgroud)
NB的else情况下,我检查的感实性input.type的情况下,输入如下所示:
<input />
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它默认 type="text".我只包含了这个,因为我不能100%确定每个浏览器"text"在关闭属性时是否会返回.至于记录,至少Chrome确实如此.http://jsfiddle.net/mattball/jJsjn/
| 归档时间: |
|
| 查看次数: |
7659 次 |
| 最近记录: |