本文介绍了附加功能左键单击< listbox>项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的firefox插件中,我有一个< listbox> 。当我左键单击框中的项目时,我希望能够使用JavaScript函数。该函数应该检索项目的文本值。

现在,我已经尝试添加此JavaScript,但它不会触发 - 相反,我得到一个错误说: :

  listbox.addEventListener(click,function(event){
var target = event.target;
while(target& target.localName!=listitem)
target = target.parentNode;
if(!target)
return; //事件目标不是列表项目
alert(target.getAttribute(value));
},false);

xul会像这样:

 < listbox id =listbox1> 
< listcols />< listcol flex =1/>< listcol flex =1/>< / listcols>
< listitem>< listcell class =column1label =label1value =value1< / listcell>< listcell label =cell1>< / listcell>< / listitem> ;
< listitem>< listcell class =column2label =label2value =value2< / listcell>< / listitem>< listcell label =cell2>< / listcell> ;
< / listbox>

现在,我有两个 listbox 元素我的xul。如何附加函数?

解决方案

您需要定义一个名为 listbox

  var listbox 

...然后您需要为它指定一个DOM元素的引用...

  var listbox = document.getElementById(listbox); 

...例如。

不要依赖某些浏览器将 id 属性添加到窗口对象中。


In my firefox addon I have a <listbox>. I want to be able to work a javascript function when I left-click on an item in the box. The function should retrieve the item's textual value.

Now, I've tried adding this javascript, but it doesn't fire - rather, I get an error saying, "listbox is undefined":

listbox.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.getAttribute("value"));
}, false);

The xul goes something like this:

<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>

Now, I have two listbox elements in my xul. How can I attach the function?

解决方案

You need to define a variable named listbox...

var listbox

...and then you need to assign it a reference to a DOM element...

var listbox = document.getElementById("listbox");

...for example.

Don't rely on some browsers adding id attributes to the window object.

这篇关于附加功能左键单击&lt; listbox&gt;项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:58