Calling a method from XBL

↘锁芯ラ 提交于 2019-12-20 05:39:08

问题


From a XBL method, when I need to call another method, I do like:

        <method name="myMethod_1">
            <body>
                <![CDATA[
                    // do staff
                ]]>
            </body>
        </method>


        <method name="myMethod_2">
            <body>
                <![CDATA[
                    document.getElementById("thisElementID").myMethod_1();
                ]]>
            </body>
        </method>

I would like to know if is there a way to call the local method without need the element id? I've tried this.myMethod_1() but it says the method don't exist.


回答1:


can you show us code calling myMethod_2? If you call it like: document.getElement(...).myMethod_2() that's fine, but if you have something like someElement.addEventHandler("click", myxbl.myMethod_2,...); that won't work since event target will be this.

This is important for determining what is this in that context

EDIT: (Tom's reply)

ow, think I got it.. it's exactly this the problem.. I'm calling it from a keypress listener of another document, and the "this" was not what I think..




回答2:


In the specific case of an event listener, there is another way around the problem, and that is to pass the element itself as the listener. Of course you only get one handleEvent method, so this is less useful if you're listening to lots of different events on lots of different event targets.

<implementation implements="nsIDOMEventListener">
  <method name="handleEvent">
    <parameter name="aEvent"/>
    <body>
      <![CDATA[
        // do stuff
      ]]>
    </body>
  </method>


来源:https://stackoverflow.com/questions/4499500/calling-a-method-from-xbl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!