jQuery创建具有属性差异的元素 [英] jQuery create element with attribute differences

查看:171
本文介绍了jQuery创建具有属性差异的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现了一些东西,并且正在研究一种方法为何有效而另一种无效的煽动性.看起来只是IE7的一件事,但是长叹了IE7,仍然需要我工作的应用程序中的一些支持.

Discovered something and am looking into a bit of incite as to why one way works and the other doesn't. Looks to be only an IE7 thing but as IE7, sigh, still needs some support in the apps I work in.

在IE7中有效的方式

var month = jQuery('<input/>');
month.attr('id', 'DOBmonth');
month.attr('title', 'Enter month');
month.attr('type', 'text');
month.attr('size', '1');
month.attr('maxlength', '2');
month.attr('class', 'numbersOnly');
month.attr('value', mm);

这种方式不起作用

var month = jQuery('<input/>', {
        id: 'DOBmonth',
        title: 'Enter month',
        type: 'text',
        size: 1,
        maxlength: 2,
        class: 'numbersOnly',
        value: mm
        });

任何人都知道为什么在IE7中只能使用一种方法,而在IE8 +,FF,Chrome和Safari中都可以用这种方法.

Anyone have an idea why only one way works in IE7 but either is fine in IE8+, FF, Chrome and Safari.

推荐答案

答案可以在 函数本身.

注意:Internet Explorer不允许您创建输入或 按钮元素并更改其类型;您必须使用指定类型 例如<input type="checkbox" />.这可以示范 在下面看到:

Note: Internet Explorer will not allow you to create an input or button element and change its type; you must specify the type using <input type="checkbox" /> for example. A demonstration of this can be seen below:

在IE中不受支持:

$('<input />', {
    type: 'text',
    name: 'test'
}).appendTo("body");

受支持的解决方法:

$('<input type="text" />').attr({
    name: 'test'
}).appendTo("body");

这篇关于jQuery创建具有属性差异的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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