Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将id添加到可见元素的子元素_Javascript_Jquery_Attributes_Add - Fatal编程技术网

Javascript 将id添加到可见元素的子元素

Javascript 将id添加到可见元素的子元素,javascript,jquery,attributes,add,Javascript,Jquery,Attributes,Add,如何在jQuery中编写以下内容: 如果.horse可见,则将#cat添加到.dog(但仅添加到.dog可见.horse的子对象) 我们可以结合设置id: $('.horse:visible .dog').attr('id', 'cat'); 这将为可见的.horse元素中包含的.dog元素提供一个id的“cat”。我们可以结合设置id: $('.horse:visible .dog').attr('id', 'cat'); 这将为可见的.horse元素中包含的.dog元素提供一个id“c

如何在jQuery中编写以下内容:

如果
.horse
可见,则将
#cat
添加到
.dog
(但仅添加到
.dog
可见
.horse
的子对象)


我们可以结合设置
id

$('.horse:visible .dog').attr('id', 'cat');
这将为可见的
.horse
元素中包含的
.dog
元素提供一个
id
的“cat”。

我们可以结合设置
id

$('.horse:visible .dog').attr('id', 'cat');
这将为可见的
.horse
元素中包含的
.dog
元素提供一个
id
“cat”。

试试

上面的代码将
#cat
附加到
.dog
中,后者是
visible.horse
的后代

如果要向特定元素添加id,请执行以下操作:

$('.horse:visible .dog').attr('id','cat');
试试看

上面的代码将
#cat
附加到
.dog
中,后者是
visible.horse
的后代

如果要向特定元素添加id,请执行以下操作:

$('.horse:visible .dog').attr('id','cat');

使用以下命令,它将起作用

$('.horse:visible .dog').attr('id','cat')

使用以下命令,它将起作用

$('.horse:visible .dog').attr('id','cat')

如果要将ID
cat
添加到
.dog
,请使用以下命令:

$(".horse:visible .dog").attr("id", "cat");

如果要将ID
cat
添加到
.dog
,请使用以下命令:

$(".horse:visible .dog").attr("id", "cat");

$(“#猫”).appendTo(“.horse:visible”)
-使用要将id
cat
添加到
.dog
的选择器id,或将id
cat
的单独元素附加到
.dog
?@ZougenMoriver是的,他/她把我们搞糊涂了\@RajaprabhuAravindasamy但看到所有答案如何从
.append()
变为
.attr()
:)我很喜欢这个问题的语义。。。一匹马可以生一只变异的猫狗!但只有当它是一匹活马,而不是一匹幽灵马时。鬼马不可能生猫狗。
$(“#猫”).appendTo(“.horse:visible”)
-使用要将id
cat
添加到
.dog
的选择器id,或将id
cat
的单独元素附加到
.dog
?@ZougenMoriver是的,他/她把我们搞糊涂了\@RajaprabhuAravindasamy但看到所有答案如何从
.append()
变为
.attr()
:)我很喜欢这个问题的语义。。。一匹马可以生一只变异的猫狗!但只有当它是一匹活马,而不是一匹幽灵马时。鬼马不可能生猫狗。