一个按钮元素上的jQuery .attr('type','submit')给出了IE7中的一个奇怪的错误 [英] jQuery .attr('type', 'submit') on a button element giving me a strange error in IE7

查看:138
本文介绍了一个按钮元素上的jQuery .attr('type','submit')给出了IE7中的一个奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 $。ajax()解析JSON响应,并从此对象的值构建一个表单。我写的脚本很长,但这里正在做什么:

I'm parsing a JSON response via $.ajax() and building a form from this object's values. The script I've written is long, but here's what it's doing:


  1. 动态创建:

    〜一个表单元素,

    〜一个字段集元素,

    〜一个按钮元素,

    〜20个左右的文本输入和标签元素

  1. Dynamically creating:
    ~ a form element,
    ~ a fieldset element,
    ~ a button element,
    ~ 20 or so text inputs and label elements

将输入和标签附加到字段集

Appending the inputs and labels to the fieldset

将按钮添加到字段集

将字段集附加到表单

除了IE中的一个小代码片段,所有浏览器都可以使用。我把它缩小到下面的代码段。 ( doc 是一个包含文档的变量

Everything is working in all browsers except one small snippet in IE. I've narrowed it down to the following piece of code. (doc is a variable containing document)

fieldset.append(
    $(doc.createElement('button'))
        .addClass('ui-button')
        .attr('type', 'submit')
        .html('Re-Rate')
        .button()
);

这是从上面的第3步。它创建一个按钮元素,添加一个类,将类型属性设置为 submit ,给出一些文本,然后将其附加到现场集。 IE失败,出现错误对象不支持此操作

This is step 3 from above. It creates a button element, adds a class, sets the type attribute to submit, gives it some text, and then appends it to the fieldset. IE fails with the error "Object doesn't support this action"

如果我注释掉 .attr()这样的行:

If I comment out the .attr() line like this:

fieldset.append(
    $(doc.createElement('button'))
        .addClass('ui-button')
        //.attr('type', 'submit')
        .html('Re-Rate')
        .button()
);

一切都符合预期。

如果你想知道, .button()方法是jQuery UI

If you're wondering, the .button() method is jQuery UI

推荐答案

p> jQuery 不允许您更改键入< input> < button> 元素一>。

jQuery doesn't allow you to change the type of an <input> or <button> element.

原因是一致性,IE插入后不允许您更改类型进入DOM。

The reason for this is consistency, and IE doesn't allow you to change the type once it's been inserted into the DOM.

这篇关于一个按钮元素上的jQuery .attr('type','submit')给出了IE7中的一个奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆