使用jquery在标签中插入'rel'属性

时间:2022-11-19 15:25:47

I'll preface this by saying that I'm a total jquery newbie. That said, I'd like to use jquery to insert a 'rel' attribute with a value of "shadowbox" into the <a> tag in the following bit of code...

我将通过说我是一个完全jquery新手来作为序言。也就是说,我想使用jquery将值为“shadowbox”的'rel'属性插入到以下代码中的标签中...

<div class="bibImage" title="">
<a href="" target="_parent"><img src="" border="0" alt=""></a>
</div>

This is system-generated html, so I don't have the option of adding 'class' or 'id' attributes which would help jquery target the element. The div is uniquely identified, so I'm guessing that it's possible to specify the <a> as a child(?); but I'm really not sure how. Nor do I understand which method can be used to actually insert the 'rel' attribute + value.

这是系统生成的html,所以我没有选择添加'class'或'id'属性,这有助于jquery定位元素。 div是唯一标识的,所以我猜测可以将指定为子(?);但我真的不确定如何。我也不明白可以使用哪种方法来实际插入'rel'属性+值。

Thanks in advance for any insight and guidance.

提前感谢任何见解和指导。

3 个解决方案

#1


4  

$(function() { // this signifies when the DOM is ready
   $('.bibImage a').attr('rel', 'shadowbox'); // this will add it to 
                                              // all <a> inside the <div>
});

#2


1  

Try this

   $('.bibImage a').attr('rel', 'shadowbox');

#3


1  

$('.bibImage a:first-child').attr('rel', 'shadowbox');

#1


4  

$(function() { // this signifies when the DOM is ready
   $('.bibImage a').attr('rel', 'shadowbox'); // this will add it to 
                                              // all <a> inside the <div>
});

#2


1  

Try this

   $('.bibImage a').attr('rel', 'shadowbox');

#3


1  

$('.bibImage a:first-child').attr('rel', 'shadowbox');