2
所以基本上我有「傑克Shephard」在'div.actor名稱'&我想要相同的值是'div.actor-image a'的title屬性。 「約翰洛克」也是如此,所以有很多情況。複製一個div的.html()並將其作爲標題屬性傳遞給<a>通過jQuery標記
JQuery的:
<script type="text/javascript">
$().ready(function() {
var actortitle = $("div.actor-image a").closest.('div.actor-name a').html();
$("div.actor-image a").each(function() {
$(this).attr('title', actortitle);
});
});
</script>
HTML:
<div id="actor1" class="lost">
<div class="actor-name">
<a href="http://www.lost.com">
Jack Shephard
</a>
</div>
<div class="actor-roles">
ROLES GO HERE
</div>
<div class="actor-image">
<a title="Jack Shephard" id="actor1-image" src="jack.jpg" href="http://www.lost.com"></a>
</div>
</div>
<div id="actor2" class="lost">
<div class="actor-name">
<a href="http://www.lost.com">
John Lock
</a>
</div>
<div class="actor-roles">
ROLES GO HERE
</div>
<div class="actor-image">
<a title="John Lock" id="actor2-image" src="john.jpg" href="http://www.lost.com"></a>
</div>
</div>
目前jQuery是無法正常運轉的預期,是推動我瘋了!
謝謝帕特里克,作品像一個魅力和非常有幫助的答案。我在想,函數的目的是什麼(i,val)? – Wonka
@移動禮物:好問題。我沒有在這個答案中使用它們,但'i'是當前元素的索引(因爲這將應用於每個找到的元素)。 'val'是'title'屬性的當前值(如果有的話)。 – user113716
謝謝帕特里克:) – Wonka