getElementById是XBL绑定的XUL元素 [英] getElementById a XBL bound XUL element

查看:153
本文介绍了getElementById是XBL绑定的XUL元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问XBL绑定的XUL元素。我想从javascript控制它。

I'm trying to access a XUL element which was XBL bound. I want to controll it from the javascript.

这是绑定。

<binding id="imip-throbber">
    <resources>
          <stylesheet src="chrome://lightning/skin/lightning-widgets.css"/>
    </resources>
    <content pack="center" align="center">
        <xul:image anonid="loading-throbber" src="chrome://global/skin/icons/loading_16.png" hidden="false"/>
    </content>
</binding>

这是CSS绑定

imip-throbber {
  -moz-binding: url(chrome://lightning/content/lightning-widgets.xml#imip-throbber);
}

这是xul实现

<

lightning-notification-bar id="imip-bar"
                                    collapsed="true"
                                    insertbefore="msgHeaderView"
                                    label="&lightning.imipbar.description;">

          <button id="imip-button1"
                  class="imip-button"
                  hidden="true"/>
          <button id="imip-button2"
                  class="imip-button"
                  hidden="true"/>
          <button id="imip-button3"
                  class="imip-button"
                  hidden="true"/>

    <imip-throbber id="loading-lbl">
</lightning-notification-bar>

这是javascript访问

This is the javascript access

let imipThrobble = document.getElementById("loading-lbl");

代码低于javascript行似乎没有被执行。我是否需要在绑定处添加一些内容才能访问该元素?

code below that javascript line not seemed to be executed. Do I need to add something at the binding to access the element?

推荐答案

不能轻易......

的rel =nofollow>文档说明了XBL自动添加的匿名内容:

The documentation on XBL at MDN says this about the "anonymous" content automatically added by XBL:


内容标记 [XBL绑定xml文件] 中的元素是匿名添加到滚动条的
。虽然屏幕上显示的是匿名内容
,但您无法通过
正常方式中的脚本访问它。对于XUL来说,就好像只有一个元素,
,即使它实际上是由许多元素组成的。

The elements within the content tag [of the XBL binding xml file] are added to the scroll bar anonymously. Although anonymous content is displayed on screen, you cannot get to it through a script in the normal way. To the XUL, it's as if there was only one single element, even though it is really made up of a number of elements.

(强调我的)。

无论如何......

MDN上的XBL 1.0参考继续说

The XBL 1.0 reference at MDN goes on to say


实际上,匿名内容存在于其自己的隔热口袋中
内该文件。使用parentNode,匿名内容节点可以
引用他们的显式父母,但显式父母对他们的匿名孩子没有
的知识。匿名内容不是
可以通过绑定元素的childNodes列表访问,也不是
可以使用firstChild / nextSibling来迭代绑定元素的子
。匿名内容只能通过特殊方法访问
,例如
getAnonymousNodes
getAnonymousElementByAttribute

所以你可以使用 getAnonymousNodes / getAnonymousElementByAttribute 获取XBL绑定自动插入的匿名内容。这些方法主要由绑定本身的(或与之相关的)JavaScript代码使用,但是,您可以从绑定到XUL DOM的JavaScript中使用它们。 MDN的一些示例

So you can use getAnonymousNodes / getAnonymousElementByAttribute to get at the anonymous content auto-inserted by the XBL binding. These methods are primarily used by the JavaScript code in (or related to) bindings themselves, however, you can use them from JavaScript bound to the XUL DOM. Some examples at MDN.

但为什么?

更好的问题是,为什么你想从绑定到XUL DOM的JavaScript访问这些元素吗?

A better question would be, why do you want to access these elements from JavaScript bound to the XUL DOM?

一个很好的理由可能是你没有编写绑定,并且你希望动态地用一些不同的内容/行为升级它们。另一方面,如果你做了编写了有问题的XBL绑定,为什么不在绑定到XBL元素的JavaScript中完成你想要做的事情呢?您可以将JavaScript 方法添加到您的已经可以访问匿名内容的元素(以及XUL DOM的其余部分)。

A good reason might be that you didn't write the bindings and you wish to "upgrade" them with some different content/behaviour on the fly. If, on the other hand, you did write the XBL bindings in question, why not accomplish what you're trying to do within the JavaScript bound to the XBL element? You can add JavaScript methods to your elements which already have access to the anonymous content (and the rest of the XUL DOM too).

这篇关于getElementById是XBL绑定的XUL元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆