gpt4 book ai didi

jQuery 创建具有属性差异的元素

转载 作者:行者123 更新时间:2023-12-03 22:19:27 26 4
gpt4 key购买 nike

发现了一些东西,并且正在研究一些关于为什么一种方法有效而另一种方法无效的煽动。看起来只是 IE7 的事情,但作为 IE7,唉,我工作的应用程序仍然需要一些支持。

在 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 中都可以。

最佳答案

答案可以在 jQuery() 的 API 中找到函数本身。

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:

Unsupported in IE:

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

Supported workaround:

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

关于jQuery 创建具有属性差异的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9898442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com