开发者

Manually Calling an event function using JQuery

I want to know if their is an option to开发者_Python百科 call a function that is already assigned to a JQuery event.

For example:

$("#somediv").change(function() { //do something });

I want to be able to manually call that function that was originally assigned to the "somediv" element.

Thanks.


Use trigger:

$("#somediv").trigger("change");


Have you tried this?

$("#somediv").change();


JQuery can trigger a given event; just call $("#somediv").change().


I think what you meant to do is define the function outside change(..).

function whatever() { // do something }

$("#somediv").change(whatever);

Then you can call your function elsewhere.

At least that's how I interpreted your question.


If you don't want to actualy trigger the change event (as shown in the previous answers above), you should probably extract that function into something that you can call from multiple places. It looks like you have found a place where reuse is in your application and extracting it may be the best answer.


A simple way of achieving this is placing the "actual" function outside the Jquery call. So, your code would look something like:

$("#somediv").change(function() { doSomething(); });

var doSomething = function() {
// actually do something
}

This would make it quite simple to call the doSomething function form anywhere in your scripts.


<script type="text/javascript">
window.onload = function () {
    $("#somediv").change();
}
</script>

i think this what you want to do .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜