我正在jQuery中構建一個自定義下拉菜單。 現在我想將A.select元素的文本設置爲單擊元素的文本。但我無法讓它工作。如何在jQuery中設置元素的父級的父級文本()?
這是HTML:
<div class="pulldown">
<a href="javascript:;" class="select">All cats/subs</a>
<div class="options">
<a href="javascript:;">Option one A</a>
<a href="javascript:;">Option two A</a>
<a href="javascript:;">Option three A</a>
<a href="javascript:;">Option four A</a>
</div>
</div>
這是jQuery代碼:
$(document).ready(function() {
$('div.pulldown').toggle(function() {
$(this).css('z-index','110');
$('.options', this).css('z-index','100').show('fast');
$('.options A', this).click(function(){
var value = $(this).text();
$(this).closest('DIV.pulldown A.select').text(value);
});
}, function(){
$(this).css('z-index','10');
$('.options', this).css('z-index','0').hide('fast');
}); });
缺少什麼我在這裏?
那裏的toggle()函數是什麼?在我看來,在每一個toggle()之後,你都要設置一個新的click()處理函數。這看起來不正確。你有沒有把console.log()轉到$(this).text()的內容,以確保你確實找到了正確的東西?在我看來,在開始進一步診斷問題之前,您必須回答這兩個問題。 – 2010-10-20 15:03:13