Jquery .val() 浏览器差异
声明
使用 jquery,如果您有一个按钮元素定义如下:
如果您使用 .attr( '值');
或 .val();
The reason?
A
<button>
element is simply <input type="button">
in a shorthand.设置 值的方法是设置
value="Click Me"
因此 与
相同。
The Question
Why does using
.val()
return different values on different browser. Who is correct?Firefox:
.val() = 123 | displayed text = 456
Chrome:
.val() = 123 | displayed text = 456
>= Internet Explorer 8:
.val() = 123 | displayed text = 456
<= Internet Explorer 7:
.val() = 456 | displayed text = 456
The Statement
With jquery if you have a button element defined as follows: <button value="123">456</button>
Browsers will give you different values if you use either .attr('value');
or .val();
The reason?
A
<button>
element is simply <input type="button">
in a shorthand.The way you would set the value of <input type="button">
is to set value="Click Me"
Hence <input type="button" value="Click Me">
is the same as <button>Click Me</button>
.
The Question
Why does using
.val()
return different values on different browser. Who is correct?Firefox:
.val() = 123 | displayed text = 456
Chrome:
.val() = 123 | displayed text = 456
>= Internet Explorer 8:
.val() = 123 | displayed text = 456
<= Internet Explorer 7:
.val() = 456 | displayed text = 456
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读 w3 规范 我被引导到相信Chrome和Firefox是正确的。具体是因为这个片段:
W3 规范将
我还决定在 Mac 上测试 Safari 5、Opera、Chrome 和 Firefox 3.6.8。全部返回 456 作为显示的文本,并返回 123 作为
.val()
Reading over the w3 specification I'm led to believe that Chrome and Firefox are correct. Specifically because of this this snippet:
The W3 spec is referring to the information found between the
<button></button>
tags as content rather than value, which is what leads me to my observation. Another hint is that Internet Explorer is the only one returning a different value, and I'm fairly certain that Internet Explorer is the root of all anguish in the universe.I also decided to test Safari 5, Opera, Chrome and Firefox 3.6.8 all on a Mac. All return 456 as text displayed and 123 as
.val()
我使用的是 IE 8.0.6001.*,它的行为与 Firefox 完全一样 - http://www .jsfiddle.net/AKGsd/3/
您可能已经发现了 Internet Exploiter 旧版本的另一个错误/功能......叹息!
I am on IE 8.0.6001.* and it is behaving exactly like Firefox does - http://www.jsfiddle.net/AKGsd/3/
You've probably uncovered yet another bug/feature of Internet Exploiter older versions... Sigh !