javascript 当包含 jQuery 时,HTML 选择元素类型为“select-one”。它是跨浏览器的价值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4655125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:26:49  来源:igfitidea点击:

HTML select element type is 'select-one' when jQuery is included. Is it a cross browser value?

javascripthtmldomhtml-select

提问by Jach Many

When I alerted the type of a select element in JavaScript it displayed 'select-one'. But I thought it would display an empty string.

当我在 JavaScript 中提醒选择元素的类型时,它显示“select-one”。但我认为它会显示一个空字符串。

alert(document.getElementById('catsel').type) 
// where catsel is a select box. it displayed select-one

I tested this in Firefox 3.0.0.10

我在 Firefox 3.0.0.10 中对此进行了测试

Is it a cross browser value? I have never used this property until now. I just want to know whether the value select-one is the same in all browsers.

它是跨浏览器的价值吗?直到现在我从未使用过这个属性。我只想知道值 select-one 在所有浏览器中是否都相同。

Besides I am using jQuery in my page. When I searched throughout the project for the string "select-one" matches were found in jquery.js. So I conclude that when the page had loaded jQuery is setting a property 'type' to the select elements. Am I right?

此外我在我的页面中使用 jQuery。当我在整个项目中搜索字符串“select-one”时,在 jquery.js 中找到了匹配项。所以我得出结论,当页面加载时,jQuery 正在为选择元素设置一个属性“类型”。我对吗?

回答by Yi Jiang

The typeproperty is actually a DOM property native to form input elements, and doesn't have anything to do with jQuery - you can quickly corroborate this by running this on anywebsite:

type属性实际上是表单输入元素的原生 DOM 属性,与 jQuery 没有任何关系 - 您可以通过在任何网站上运行它来快速证实这一点:

console.log(document.createElement('select').type);

For selectelements, the two possible values it could take is select-onefor normal select elements, ad select-multiplewhen more than one value is accepted (ie. when a valid multipleattribute is set).

对于select元素,它可能采用的两个可能值是select-one用于普通选择元素,select-multiple当接受多个值时(即当multiple设置了有效属性时)。

The value should be cross-browser compatible - I could not find any information disputing this.

该值应该是跨浏览器兼容的 - 我找不到任何对此有争议的信息。

Reference: https://developer.mozilla.org/en/DOM/select.type

参考:https: //developer.mozilla.org/en/DOM/select.type