2010-09-10 72 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> 
相關問題