0

在 HTML 中我有这个:

<div id="content">
    what is inside
</div>
<a title='content'>inside my link</a>

使用 JQuery,我想向 a 元素添加一个属性,其值是 a 元素的标题值中引用的元素的值。

我想过,但显然 $(this) 不是我想的那样:

$('a').attr('myNewAttribute', $('#' + $(this).attr('title')).text().trim());

myNewAttribute 为空

4

1 回答 1

2

您需要在此处使用.attr()的不同变体。

$('a').attr('myNewAttribute', function(){
    return $('#' + $(this).attr('title')).text().trim()
});

演示:小提琴

于 2013-07-30T12:10:03.937 回答