HTML Javascript物理点击触发事件但逻辑点击没有

时间:2015-08-27 15:24:22

标签: javascript html events triggers onclick

我有这个按钮:

<button class="dijitStretch dijitButtonNode dijitButtonContents dijitButton jStopThreads dijitButton" dojoattachevent="onclick:_onButtonClick,onmouseenter:_onMouse,onmouseover:_onMouse,onmouseout:_onMouse,onmouseleave:_onMouse,onmousedown:_onMouse" dojoattachpoint="focusNode,titleNode" type="button" wairole="button" waistate="labelledby-jStopThreads_label" role="button" aria-labelledby="jStopThreads_label" id="jStopThreads" tabindex="0" widgetid="jStopThreads" aria-valuenow="" aria-disabled="false" style="position: absolute; left: 76px; top: 528px; z-index: 3; width: 104px; height: 20px;"><span class="dijitInline " dojoattachpoint="iconNode"><span class="dijitToggleButtonIconChar">✓</span></span><span class="dijitButtonText" id="jStopThreads_label" dojoattachpoint="containerNode">

当我实际点击该按钮时,所需的事件触发正确,但当我以这种方式使用jQuery时它不会:

$('.jStopThreads').trigger("click");

$('.jStopThreads').click();

按钮点击是因为它会聚焦,但是相关事件不会被触发?这怎么可能?

2 个答案:

答案 0 :(得分:0)

那是因为jQuery只能触发通过jQuery添加的事件。 所以,如果你想通过jQuery添加这样的事件:

$('.jStopThreads').click(function() {
  // the content of your function
});

稍后可以通过jQuery函数trigger()或click()

调用它

答案 1 :(得分:0)

看起来您正在使用库Dojo来附加您的点击事件。在jQuery中执行:

function clickHandler(e) {
    alert('clicked!');
}

$('.jStopThreads').on('click', clickHandler);

$('.jStopThreads').trigger("click");

http://jsfiddle.net/