Jquery 向绑定函数发送数据。“与结构相关”;单击:函数(){…}";
我可以发送这样的数据(数据不同) 但在这种结构下:Jquery 向绑定函数发送数据。“与结构相关”;单击:函数(){…}";,jquery,events,bind,Jquery,Events,Bind,我可以发送这样的数据(数据不同) 但在这种结构下: $("div.test").bind({ click: function(){ ... }, mouseenter: function(){ ... }, mouseleave: function(){ ... } }); 是否也可以发送相同的信息,“但在使用数据声明变量之前: 不是这个: var data = "test"; $("div.test").bind({ click: function(){ // use
$("div.test").bind({
click: function(){ ... },
mouseenter: function(){ ... },
mouseleave: function(){ ... }
});
是否也可以发送相同的信息,“但在使用数据声明变量之前:
不是这个:
var data = "test";
$("div.test").bind({
click: function(){
// use data
},
mouseenter:{
// use data
},
mouseleave: {
// use data
}
});
谢谢我不知道它为什么重要,但是如果您不想让它再次运行选择器,您可以做两件事:
1) 绑定前保存对象:
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) 你可以把它们链接起来:
$("div.test").bind("click", {foo: "bar1"}, function(event) { ... })
.bind("mouseenter", {foo: "bar2"}, function(event) { ... })
.bind("mouseleave", {foo: "bar3"}, function(event) { ... });
你想这样做还有其他原因吗
编辑要明确回答这个问题,不,如果不使用jQuery“map”对象格式对外声明数据,就不可能传入数据。除非手动触发事件,我认为这不是您想要做的。好吧,这不是我想要的,但对我很有用: HTML
测试
JS
var fn=函数(事件){
$(“#数据事件”).append(“+event.data[event.type].xx+”);
};
$(“#测试”).bind(
{
点击:fn,
鼠标盖:fn
},
{点击:{xx:“点击文本”},鼠标悬停:{xx:“鼠标悬停文本”});
这是一个json结构,在使用和维护方面有很大不同statement@andresdescalzo-我知道这是一个JSON结构,但根据您的示例,您正在做的事情与使用上面的示例绑定它们一样“版本与为每个事件多次调用绑定相同。@andres descalzo-什么?我想我应该明确指出这是不可能的。我在now.BTW中添加了这一点,它不是一个“JSON结构”。JSON对象不能包含函数。它是一个普通的JavaScript对象。
$("div.test").bind("click", {foo: "bar1"}, function(event) { ... })
.bind("mouseenter", {foo: "bar2"}, function(event) { ... })
.bind("mouseleave", {foo: "bar3"}, function(event) { ... });
<button id="test">test</button>
<div id="data-event"></div>
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" } });