jquery - 在 IE 中使用 jQuery 获取属性

标签 jquery internet-explorer attributes

我有简单的按钮:

<button type="button" id="somebutton" value="start">blah</button>

在 jQuery 中,在 bind('click, function(){... 我有这个:

$(this).attr("value")

当然,它的作用是获取属性的值,在本例中为:“start”。好的,这在 Firefox 中有效,我得到了正确的“start”值。然而在 IE 7 中我得到的值是“somebutton”。

为什么?

最佳答案

我知道这是不久前发布的,但以防万一有人正在寻找答案......

IE 将 .val() 和 .attr("value") 报告为按钮元素的文本标签(内容),而不是 value 属性的实际值。

您可以通过暂时删除按钮的标签来解决此问题:

var label = $(this).text(); 
$(this).text('');
var buttonValue = $(this).val();
$(this).text(label);

IE 中的按钮还有一些其他的怪癖。我已发布a fix for most of the issues here .

关于jquery - 在 IE 中使用 jQuery 获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/888653/

相关文章:

javascript - html 元素的单元测试用例

css - ie/chrome 中的 formalize.css 按钮

c# - ServiceBehavior 属性是否被其他类继承?

OpenGL 3/4 glVertexAttribPointer 步长和偏移计算错误

c# - ASP.Net MVC 2.0 : EditorFor setting name via attributes

jquery - 在 jQuery 函数中选择类 a :active span vs. a.active 跨度?

javascript - Bootstrap 4 表单验证

javascript - $compile 动态添加行以使用 ngClick - AngularJS

html - 如何为 IE 设置边框不透明度?

javascript - 强制 IE 在加载时重新下载文本文件