简体   繁体   English

使用jQuery在Firefox附加组件中检查和取消选中菜单项

[英]Checking and unchecking menuitem in a Firefox add-on with jQuery

I have a <menuitem type="checkbox" checked="true" label="Enabled"> element in the Firefox add-on I'm working on. 我正在使用的Firefox加载<menuitem type="checkbox" checked="true" label="Enabled">有一个<menuitem type="checkbox" checked="true" label="Enabled">元素。 I'm using jQuery to set and unset the checked attribute, like so: 我正在使用jQuery设置和取消设置checked属性,如下所示:

var element = $("#extension-menupopup:first-child");
element.attr("checked", "false");
element.attr("label", "Disabled");
element.attr("tooltiptext", "Extension is disabled.");

The first two .attr() calls seem to work, but the tick mark beside "Enabled" does not disappear, and the label does not change to "Disabled". 前两个.attr()调用似乎可以正常工作,但是“已启用”旁边的对勾标记不会消失, label也不会更改为“已禁用”。 Verifying the values on Scratchpad's "Browser context", the values are returned as "false" and "Disabled", respectively. 验证Scratchpad的“浏览器上下文”上的值,这些值分别返回为“ false”和“ Disabled”。 So, the values of the attributes are being set properly, but the UI does not update to show these changes. 因此,属性的值已正确设置,但是UI不会更新以显示这些更改。 The last line successfully sets the tooltiptext text properly and the UI change is visible immediately. 最后一行成功地正确设置了工具tooltiptext文本,并且UI更改立即可见。 What should I do to make the changes visible in the UI? 如何使更改在用户界面中可见?

It seems that jQuery is not suited for this particular purpose, although it works just fine in other situations. jQuery似乎不适合该特定目的,尽管它在其他情况下也可以正常工作。 The way to go about changing the label and checked attributes is to do it the normal javascript way: 更改labelchecked属性的方法是使用常规的javascript方法:

//get the element that needs to be modified
var element = document.getElementById("extension-menupopup").childNodes[0];
element.setAttribute("checked", false);
element.setAttribute("label", "Disabled");

Also, be aware of this bug to avoid any gotchas. 另外,请注意此错误,以避免产生任何麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM