<! DOCTYPE html>
<script src= "https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js" >
</script>
<script>
Function F1 (i) {
Alert ("the paragraph is clicked. "+i.data.this);
}
$ (document). Ready (function () {
$ ("p"). on ("click", {this:5},f1);
});
</script>
<body>
<p> Click this paragraph. </p>
</body>
. On (events [, selector] [, data], handler (eventobject))
handlerThe argument must be a function (or false value, see Below) unless you pass an object to the events Argument. You can provide an anonymous handler for the .on() call, as in the example above, or you can declare a function and then use the function name as an argument:
| 12 |
function notify() { alert("clicked"); }$("button").on("click", notify); |
If data the parameter is given .on() and is not null or undefined , then each time the event is triggered, it event.data is passed to the Handler. dataThe parameter can be of any type, but if it is a string type, then selector the parameter must be provided, or explicitly passed null , so that the data parameter is not mistaken for a selector. It is best to use an object (a key-value pair), so you can pass multiple values as Attributes.
jquery 1.4, The same event handler can be bound to an element multiple Times. This event.data is especially useful for using features, or for using unique data in Closures. For example:
| 123 |
function greet(event) { alert("Hello "+event.data.name); }$("button").on("click", { name: "Karl" }, greet);$("button").on("click", { name: "Addy" }, greet); |
JQuery on () method problem