2010-09-10 71 views
2

此示例来自this Mozilla's page为什么这个XBL示例不起作用?

main.xul

<?xml version="1.0"?> 
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> 
<?xml-stylesheet href="main.css" type="text/css"?> 

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 

    <box id="num" class="labeledbutton" title="Number of Things:" value="52"/> 

    <button label="Show" oncommand="document.getElementById('num').showTitle(true)"/> 
    <button label="Hide" oncommand="document.getElementById('num').showTitle(false)"/> 
</window> 

的main.css

box.okcancelbuttons { 
    -moz-binding: url('main.xml#labeledbutton'); 
} 

main.xml中

<?xml version="1.0"?> 
<binding id="labeledbutton"> 
    <content> 
    <xul:label xbl:inherits="value=title"/> 
    <xul:label xbl:inherits="value"/> 
    </content> 
    <implementation> 
    <method name="showTitle"> 
     <parameter name="state"/> 
     <body> 
     if (state) document.getAnonymousNodes(this)[0]. 
      setAttribute("style","visibility: visible"); 
     else document.getAnonymousNodes(this)[0]. 
      setAttribute("style","visibility: collapse"); 
     </body> 
    </method> 
    </implementation> 
</binding> 

为什么不显示当我点击按钮框?

回答

4

有几个问题:

所有的main.css首先你在你指labeledbutton类main.xul定义一个类okcancelbuttons呢。该类可以被称为与绑定相同。

其次main.xml只是无效的XML(验证最简单的方法是加载它在Firefox中,它会吐出错误)。 它需要您使用的每个名称空间的xmlns属性。在这种情况下,“主”名称空间xbl和xul。它们应该在<binding>元素周围缺失的<bindings>元素中定义。

它最终会是这样的:

main.xml中

<?xml version="1.0"?> 
<bindings xmlns="http://www.mozilla.org/xbl" 
      xmlns:xbl="http://www.mozilla.org/xbl" 
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 
<binding id="labeledbutton"> 
    <content> 
    <xul:label xbl:inherits="value=title"/> 
    <xul:label xbl:inherits="value"/> 
    </content> 
    <implementation> 
    <method name="showTitle"> 
     <parameter name="state"/> 
     <body> 
     if (state) document.getAnonymousNodes(this)[0]. 
      setAttribute("style","visibility: visible"); 
     else document.getAnonymousNodes(this)[0]. 
      setAttribute("style","visibility: collapse"); 
     </body> 
    </method> 
    </implementation> 
</binding> 
</bindings> 
+0

酷!这个例子应该是这样清楚的。谢谢! – 2010-09-13 15:01:53

+0

@TomBrito,你知道,你可以编辑它;) – batman 2013-07-09 14:34:38

5

只是尝试一下

XUL(main.xul)

<box id="num" class="labeledbutton" title="Number of Things:" value="52"/> 

<button label="Show" oncommand="document.getElementById('num').showTitle(true)"/> 
<button label="Hide" oncommand="document.getElementById('num').showTitle(false)"/> 

CSS(的main.css)

box.okcancelbuttons { 
    -moz-binding: url('main.xbl#labeledbutton'); 
} 

XBL(main.xbl)

<?xml version="1.0"?> 
<bindings xmlns="http://www.mozilla.org/xbl" 
      xmlns:xbl="http://www.mozilla.org/xbl" 
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 
<binding id="labeledbutton"> 
    <content> 
    <xul:label xbl:inherits="value=title"/> 
    <xul:label xbl:inherits="value"/> 
    </content> 
    <implementation> 
    <method name="showTitle"> 
     <parameter name="state"/> 
     <body> 
     if (state) document.getAnonymousNodes(this)[0]. 
      setAttribute("style","visibility: visible"); 
     else document.getAnonymousNodes(this)[0]. 
      setAttribute("style","visibility: collapse"); 
     </body> 
    </method> 
    </implementation> 
</binding> 
</bindings> 
相关问题