如何在WinJS中触发事件(相当于jQuery中的.trigger(“”))

时间:2013-01-14 02:13:33

标签: windows-store-apps winjs

我想知道如何从代码中触发事件。在jQuery中触发“onchange”事件,你只需要$(“selector”)。触发器(“更改”)。有没有办法使用WinJS做同样的事情。

[包含jQuery的所有建议都将被丢弃]

感谢。

4 个答案:

答案 0 :(得分:3)

fireEvent可能就是你想要的。

  

在对象上触发指定的事件。

鉴于标记

<p>
    <button 
        id="oButton" 
        onclick="this.innerText='I have been clicked!'">Button
    </button>
</p>

你可以做到

oButton.fireEvent("onclick");

答案 1 :(得分:0)

在准备好的功能中:

WinJS.Utilities.query("#yourSelector").listen("change", this.customClicked, false)

然后定义customClicked函数(可以命名为任何东西)

customClicked: function(eventInfo){
   <!--do stuff -->
}

答案 2 :(得分:0)

您可以使用event.initUIEvent

var button = document.getElementById('button');
var еvent = document.createEvent("UIEvents");
еvent .initUIEvent("click", false, false, window, 1);
button.dispatchEvent(еvent );

可以找到更多信息here

答案 3 :(得分:0)

要触发并响应自定义事件,请执行此操作。

解雇事件

WinJS.Application.queueEvent({type: 'ev_login_completed'})

回复自定义事件

WinJS.Application.addEventListener('ev_login_completed', function (e) {
             // do stuff  })

HTH