有没有办法改变 jQuery 中的事件参数?
我希望能够更改传递到可选择项的启动事件中的参数,因此我基本上可以允许用户使用可选择的 jQuery UI 效果,而无需按住 CTRL 键。
JS
$(function() {
$( "#selectable" ).bind("mousedown", function(event, ui) {
var result = $( "#select-result" ).empty();
event.metaKey = event.ctrlKey = true;
});
$( "#selectable" ).selectable();
});
我对我想要在这里完成的事情有一个困惑:
http://jsfiddle.net/josephbulger/ZfevM /
我遇到的问题是,当我在启动方法中设置事件的参数时,停止方法看不到我所做的更改。
有办法完成我想做的事情吗?
I want to be able to change the parameters that are being passed into the selectable's start event, so I can basically allow my user to use the selectable jQuery UI effect without having to hold down the CTRL key.
JS
$(function() {
$( "#selectable" ).bind("mousedown", function(event, ui) {
var result = $( "#select-result" ).empty();
event.metaKey = event.ctrlKey = true;
});
$( "#selectable" ).selectable();
});
I have a fiddle with what I'm trying to accomplish here:
http://jsfiddle.net/josephbulger/ZfevM/
The problem I'm having, is that when i set the event's parameters in the start method, the stop method is not seeing the changes I'm making.
Is there a way to accomplish what I'm trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法在此处设置属性,不...因为它是
stop
方法中的不同event
对象。您可以在更高的范围内设置一些变量(像这样),但没有一个会持续存在事件
对象。这并不是说它真的在“清理”它们,它只是一个全新的对象。要使可选择的行为就像按住 Ctrl 一样,请绑定到它提前使用的
mousedown
事件,并将.metaKey
属性设置为将事件
设置为true,像这样:
你可以在这里测试一下,记得先找到 调用
.selectable()
,因为事件处理程序是按顺序绑定执行的。You can't set the properties here no...because it's a different
event
object in thestop
method. You can set some variables at a higher scope (like this), but none that will persist on theevent
object. It's not that it's "clearing" them really, it's just a brand spanking new object.To make the selectable behave as if Ctrl is held down, bind to the
mousedown
event it uses ahead of time and set the.metaKey
property on thatevent
totrue
, like this:You can test it out here, remember to find before calling
.selectable()
, since event handlers are executed in the order bound.我已经更新了代码。看看这个网址。
http://jsfiddle.net/phoenix_suresh/7f6j5/
同时,我已经为
I've updated the code. Have a look at this URL.
http://jsfiddle.net/phoenix_suresh/7f6j5/
Meanwhile, I've added ID attribute to
找到这个答案后,我注意到您可以使用可选
start
事件处理程序中返回的event
对象的originalEvent
属性来设置>ctrlKey
属性设置为 true。像这样:
工作 fiddle
After finding this answer I noticed that you can use the
originalEvent
property on theevent
object returned in the selectablestart
event handler to set thectrlKey
property to true.Like so:
Working fiddle