为什么这种类型的数据包装在jQuery中不起作用?

时间:2021-08-08 22:32:04

Consider this:

var i=$('<img src="/path/to/imgI.png"/>');
var j=$('<img src="/path/to/imgJ.png"/>');
$([i,j]).css('cursor','hand');

The cursor is not changed however and I don't know why..

但光标没有改变,我不知道为什么..

When I do it separately, it works.

当我单独做它时,它的工作原理。

Thanks.

5 个解决方案

#1


The array is of two jQuery objects when what you require is the DOM elements within those jQuery objects. This will work:

当你需要的是那些jQuery对象中的DOM元素时,数组有两个jQuery对象。这将有效:

var i=$('<img src="/path/to/imgI.png"/>')[0]; // <= Notice [0]
var j=$('<img src="/path/to/imgJ.png"/>')[0];
$([i,j]).css('cursor','pointer');

Alternatively, (using add())

或者,(使用add())

var i=$('<img src="/path/to/imgI.png"/>');
var j=$('<img src="/path/to/imgJ.png"/>');
$(i).add(j).css('cursor','pointer');

EDIT: Also, use cursor:pointer; instead of cursor:hand;

编辑:另外,使用cursor:pointer;而不是游标:手;

#2


Are you sure your problems isn't browser specific? That particular css property is tricky, it requires the property be set two different ways to work in IE and Firefox.

您确定您的问题不是特定于浏览器的吗?那个特定的css属性很棘手,它需要在IE和Firefox中设置两种不同的属性。

I'd recommend using a class in the img tag to specify the hand property. Then you can specify both rules and get what you are looking for.

我建议在img标签中使用一个类来指定hand属性。然后,您可以指定两个规则并获取您要查找的内容。

#3


Would make more sense to put selectors in the array:

将选择器放在数组中会更有意义:

var i = $('<img src="/path/to/imgI.png"/>').attr('id','i');
var j = $('<img src="/path/to/imgJ.png"/>').attr('id','j');
$( ['#i', '#j'] ).css('cursor','hand');

#4


The correct cursor property is "pointer" not "hand", which is an IE only extension no longer required for anything but IE 5.5 and lower - i.e. very rarely.

正确的游标属性是“指针”而不是“手”,这是除IE 5.5及更低版本之外的任何东西不再需要的仅IE扩展 - 即很少。

#5


You can use jQuery method to turn the jQuery object into a true array and then merge them.

您可以使用jQuery方法将jQuery对象转换为真正的数组,然后合并它们。

var i=$('<img src="/path/to/imgI.png"/>');
var j=$('<img src="/path/to/imgJ.png"/>');
i = $.makeArray(i);
j = $.makeArray(j);
$( $.merge(i,j) ).css('cursor','pointer');

Btw that also works when you need to add multiple jQuery selection together,

顺便说一句,当你需要一起添加多个jQuery选择时,它也可以工作,

i = $.makeArray( $('div') );
j = $.makeArray( $('a') );
$( $.merge(i,j) ); //this jQuery object holds all divs and a's

You could of course also do that like this:

你当然也可以这样做:

$('div').add('a');

#1


The array is of two jQuery objects when what you require is the DOM elements within those jQuery objects. This will work:

当你需要的是那些jQuery对象中的DOM元素时,数组有两个jQuery对象。这将有效:

var i=$('<img src="/path/to/imgI.png"/>')[0]; // <= Notice [0]
var j=$('<img src="/path/to/imgJ.png"/>')[0];
$([i,j]).css('cursor','pointer');

Alternatively, (using add())

或者,(使用add())

var i=$('<img src="/path/to/imgI.png"/>');
var j=$('<img src="/path/to/imgJ.png"/>');
$(i).add(j).css('cursor','pointer');

EDIT: Also, use cursor:pointer; instead of cursor:hand;

编辑:另外,使用cursor:pointer;而不是游标:手;

#2


Are you sure your problems isn't browser specific? That particular css property is tricky, it requires the property be set two different ways to work in IE and Firefox.

您确定您的问题不是特定于浏览器的吗?那个特定的css属性很棘手,它需要在IE和Firefox中设置两种不同的属性。

I'd recommend using a class in the img tag to specify the hand property. Then you can specify both rules and get what you are looking for.

我建议在img标签中使用一个类来指定hand属性。然后,您可以指定两个规则并获取您要查找的内容。

#3


Would make more sense to put selectors in the array:

将选择器放在数组中会更有意义:

var i = $('<img src="/path/to/imgI.png"/>').attr('id','i');
var j = $('<img src="/path/to/imgJ.png"/>').attr('id','j');
$( ['#i', '#j'] ).css('cursor','hand');

#4


The correct cursor property is "pointer" not "hand", which is an IE only extension no longer required for anything but IE 5.5 and lower - i.e. very rarely.

正确的游标属性是“指针”而不是“手”,这是除IE 5.5及更低版本之外的任何东西不再需要的仅IE扩展 - 即很少。

#5


You can use jQuery method to turn the jQuery object into a true array and then merge them.

您可以使用jQuery方法将jQuery对象转换为真正的数组,然后合并它们。

var i=$('<img src="/path/to/imgI.png"/>');
var j=$('<img src="/path/to/imgJ.png"/>');
i = $.makeArray(i);
j = $.makeArray(j);
$( $.merge(i,j) ).css('cursor','pointer');

Btw that also works when you need to add multiple jQuery selection together,

顺便说一句,当你需要一起添加多个jQuery选择时,它也可以工作,

i = $.makeArray( $('div') );
j = $.makeArray( $('a') );
$( $.merge(i,j) ); //this jQuery object holds all divs and a's

You could of course also do that like this:

你当然也可以这样做:

$('div').add('a');