在我的firefox插件中我有一個<listbox>
。當我左鍵單擊框中的某個項目時,我希望能夠使用JavaScript功能。該函數應該檢索項目的文本值。現在如何從listbox獲取值listitem
,我的功能確實,當我點擊一個listitem
被調用,因爲我已經放在這在我的事件偵聽器的onLoad電話:
var myListBox = document.getElementById("myListBoxID");
myListBox.addEventListener("click", function(event){
var target = event.target;
while (target && target.localName != "listitem"){
target = target.parentNode;
}
if (!target){
return; // Event target isn't a list item
}
alert(target); //returns blank
alert(target.id); //returns blank
alert(target.getAttribute("value")); //returns blank
alert(target.getAttribute("text")); //returns blank
alert(target.getAttribute("id")); //returns blank
var targetid = document.getElementById(target.id);
alert(targetid); //returns null
}, false);
},
XUL的是這樣的:
<listbox id="listbox1">
<listcols /><listcol flex="1"/><listcol flex="1"/></listcols>
<listitem><listcell class="column1" label="label1" value="value1"</listcell><listcell label="cell1"></listcell></listitem>
<listitem><listcell class="column2" label="label2" value="value2"</listcell></listitem><listcell label="cell2"></listcell>
</listbox>
但是,我無法讓它顯示項目的文本。正如你在上面看到的,我似乎沒有合適的處理target
我從這裏有gotten the original code和got the EventListener
working here。
如何獲取listcells的值?我已經嘗試了一切!
謝謝,弗拉基米爾。這對我有意義。我仍然在這裏學習。但是,現在沒有任何警報。然而,函數IS正在觸發,如果我註釋掉「if(!target)」部分,錯誤控制檯會告訴我它是'null'。但它看起來總是「空」,因爲之後沒有任何火災發生。 – bgmCoder 2013-03-04 00:35:00
你對我有另一個線索嗎? – bgmCoder 2013-03-05 15:16:12
啊!問題是目標是'listitem'而不是'listcell'。 – bgmCoder 2013-03-07 01:52:22