Jquery .val() 浏览器差异

发布于 2024-09-18 03:50:54 字数 943 浏览 7 评论 0原文

声明
使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-09-25 03:50:54

阅读 w3 规范 我被引导到相信Chrome和Firefox是正确的。具体是因为这个片段:

使用 BUTTON 创建的按钮
元素功能就像按钮一样
使用 INPUT 元素创建,但是
他们提供更丰富的渲染
可能性:BUTTON 元素可能
有内容。

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:

Buttons created with the BUTTON
element function just like buttons
created with the INPUT element, but
they offer richer rendering
possibilities: the BUTTON element may
have content.

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()

蹲墙角沉默 2024-09-25 03:50:54

我使用的是 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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文