发送数据到绑定函数。具有结构“click: function () {...}”

发布于 2024-10-01 08:13:26 字数 671 浏览 2 评论 0原文

我可以发送这样的数据(数据不同)?

$("div.test").bind("click", {foo: "bar1"}, function(event) { ... })
$("div.test").bind("mouseenter", {foo: "bar2"}, function(event) { ... })
$("div.test").bind("mouseleave", {foo: "bar3"}, function(event) { ... })

但使用这种结构:

$("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
  }
});

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随梦而飞# 2024-10-08 08:13:26

我不确定为什么它很重要,但如果您只是不想让它再次运行选择器,您可以做两件事:
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”对象格式在外部声明数据,就不可能传递数据。除非您手动触发事件,我认为这不是您想要做的。

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.

最美不过初阳 2024-10-08 08:13:26

好吧,不是我想要的,但它对我有用:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文