自定义事件,在javascript中使用自定义触发器

时间:2015-02-17 14:24:11

标签: javascript jquery events jquery-mobile custom-events

在JQueryMobile中,你可以这样做:

$(document).on("pagebeforehide","#pagetwo",function(){
  alert("pagebeforehide event fired - pagetwo is about to be hidden");
});

这可以通过自定义事件进行吗?像这样:

document.addEventListener("someevent", "sometrigger", function () {});

1 个答案:

答案 0 :(得分:1)

尝试



$(document).on("someevent", "div", function(e) {
      console.log(this.innerText)
});

var elem = $("div:contains('abc')");

if (elem.length) {
  elem.trigger("someevent")
};

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>abc</div>
<div>123</div>
&#13;
&#13;
&#13;

请参阅.trigger()