为什么这只适用于Firefox?

时间:2022-10-15 20:03:53

I've created a self-contained example to find out why this is only working in Firefox:

我已经创建了一个自包含的示例,以找出为什么它只适用于Firefox:

var ul = jQuery('<ul></ul>');

jQuery(selector).children().each(function() {
   var li = jQuery('<li></li>');
   var label = '<label for="' + this.id + '">' + this.name + '</label>';
   li.append(label);
   li.append(this);
   ul.append(li);
});

Any webkit browser or even IE fails on this line:

任何webkit浏览器甚至IE都失败了:

li.append(this);

this is a HTMLInputElement. Any ideas?

这是一个HTMLInputElement。有任何想法吗?

Thanks, Pete

2 个解决方案

#1


The problem is, that "this" is a DOM node already located at some place. You can't move DOM nodes around, that already are located somewhere without first removing or copying them.

问题是,“this”是已经位于某个地方的DOM节点。您无法移动已经位于某处的DOM节点,而无需先删除或复制它们。

If you want to:

如果你想:

  • move it, you have to do

    移动它,你必须这样做

    li.append ($(this).remove())

  • copy it, you'll do

    复制它,你会做的

    li.append ($(this).clone (true))

    li.append($(this).clone(true))

Actually, I'm quite puzzled, that it should work in FF. If it does, it's an FF bug.

实际上,我很困惑,它应该在FF中工作。如果是这样,那就是FF错误。

Cheers,

#2


Perhaps this is an issue with the element you are trying to select the children from, or an issue with the version of jQuery you are using.

也许这是您尝试从中选择子元素的元素的问题,或者是您正在使用的jQuery版本的问题。

I am using 1.3.2 and using your code (copy & pasted) I can successfully run this in FF 3, IE 8, and Chrome.

我正在使用1.3.2并使用您的代码(复制和粘贴)我可以在FF 3,IE 8和Chrome中成功运行它。

#1


The problem is, that "this" is a DOM node already located at some place. You can't move DOM nodes around, that already are located somewhere without first removing or copying them.

问题是,“this”是已经位于某个地方的DOM节点。您无法移动已经位于某处的DOM节点,而无需先删除或复制它们。

If you want to:

如果你想:

  • move it, you have to do

    移动它,你必须这样做

    li.append ($(this).remove())

  • copy it, you'll do

    复制它,你会做的

    li.append ($(this).clone (true))

    li.append($(this).clone(true))

Actually, I'm quite puzzled, that it should work in FF. If it does, it's an FF bug.

实际上,我很困惑,它应该在FF中工作。如果是这样,那就是FF错误。

Cheers,

#2


Perhaps this is an issue with the element you are trying to select the children from, or an issue with the version of jQuery you are using.

也许这是您尝试从中选择子元素的元素的问题,或者是您正在使用的jQuery版本的问题。

I am using 1.3.2 and using your code (copy & pasted) I can successfully run this in FF 3, IE 8, and Chrome.

我正在使用1.3.2并使用您的代码(复制和粘贴)我可以在FF 3,IE 8和Chrome中成功运行它。