Send data to bind functions. with the structure "click: function () {...}"
I can send data such as this (the data are different)?
$("div.test").bind("click", {foo: "bar1"}, function(event) { ... })
$("div.test").bind("mouseenter", {foo: "bar2"}, function(event) { ... })
$("div.test")开发者_运维百科.bind("mouseleave", {foo: "bar3"}, function(event) { ... })
but with this structure:
$("div.test").bind({
click: function(){ ... },
mouseenter: function(){ ... },
mouseleave: function(){ ... }
});
and is it also possible to send the same information, "but not before declaring a variable with the data:
not this:
var data = "test";
$("div.test").bind({
click: function(){
// use data
},
mouseenter:{
// use data
},
mouseleave: {
// use data
}
});
thanks
I'm not sure why it matters, but if you just don't want it running the selector over again, you can do two things:
1) Save the object before binding:
var $mytest = $("div.test");
$mytest.bind("click", {foo: "bar1"}, function(event) { ... });
$mytest.bind("mouseenter", {foo: "bar2"}, function(event) { ... });
$mytest.bind("mouseleave", {foo: "bar3"}, function(event) { ... });
2) You could chain them:
$("div.test").bind("click", {foo: "bar1"}, function(event) { ... })
.bind("mouseenter", {foo: "bar2"}, function(event) { ... })
.bind("mouseleave", {foo: "bar3"}, function(event) { ... });
Is there another reason why you want to do this?
Edit
To explicitly answer the question, no it's not possible to pass data in without declaring it outside with the jQuery "map" object format. Unless you trigger the events manually, which I don't think is what you want to do.
well, not what I wanted but it is useful to me:
HTML
<button id="test">test</button>
<div id="data-event"></div>
JS
var fn = function(event) {
$("#data-event").append("<div>" + event.data[event.type].xx + "</div>");
};
$("#test").bind(
{
click: fn,
mouseover: fn
},
{click: { xx: "click-text" }, mouseover: { xx: "mouseover-text" } });
example
精彩评论