- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在开发一个扩展 ui.mouse 的 jquery 小部件。
小部件需要通过创建元素并将它们附加到 this.element 来响应鼠标事件。
如果小部件应用于不能包含子元素的 HTML 元素(例如 IMG 标签),则小部件会创建一个代理包装器元素并包装 this.element。
如果我用 wrapper 元素替换 this.element,widget 触发的事件永远不会被处理,因为对 widget 实例的 .bind() 调用将处理程序应用到原始元素,而不是 wrapper。
如果我不将 this.element 替换为包装器元素,则 ui.mouse 定义的 _mouse* 事件将无法正确调用,因为它们应用于 this.element 而不是包装器。
是否有一种优雅的方式以某种方式返回包装器元素,以便将后续的 bind() 调用应用于它,或者使 ui.mouse 原型(prototype)应用于 this.element 以外的元素?
非常欢迎任何其他优雅的解决方案或建议。
这是我尝试过的两种情况。
$.widget("ui.example", ui.mouse, {
_containerElement: null,
_create: function ()
{
if (this.element[0].nodeName.match(/canvas|textarea|input|select|button|img/i))
{
this._applyContainer();
}else{
this._containerElement = this.element;
}
// Applies mouse interaction handlers to this.element
this._mouseInit();
},
_applyContainer: function ()
{
this.element.wrap("<span>");
this._containerElement = this.element.parent();
this._containerElement.css({position:'relative'});
},
_mouseStart: function (event) {
// Not always called because it handles mouse interaction with
// the IMG element rather than the wrapper element
this._trigger("mouseevent");
}
})
$("IMG").example().bind("examplemouseevent", function(){
//This fires but only when the original IMG element is clicked, not the wrapper
})
$.widget("ui.example", ui.mouse, {
_create: function ()
{
if (this.element[0].nodeName.match(/canvas|textarea|input|select|button|img/i))
{
this._applyContainer();
}
// Applies mouse interaction handlers to this.element
this._mouseInit();
},
_applyContainer: function ()
{
this.element.wrap("<span>");
this.element = this.element.parent();
this.element.css({position:'relative'});
},
_mouseStart: function (event) {
// This event is never handled because it is not raised on the original element.
this._trigger("mouseevent");
}
})
$("IMG").example().bind("examplemouseevent", function(){
//This never fires because the bind is to the IMG element, not the wrapper
})
我很确定我遇到的问题是由于我理解 jquery ui.mouse 工作方式的缺点,或者是 jquery 小部件框架的限制。
用包装原始 this.element 的元素替换 this.element 是可行的,但事件不会传递给稍后绑定(bind)到小部件的处理程序。
不替换 this.element 并存储对包装器的单独引用会导致 jquery 基础 ui.mouse 的行为将事件附加到错误的元素。
代码按设计运行,我的问题是找到最佳方法来处理设计限制。
我可以看到很多解决这个问题的方法。例如; duck-punching _trigger 方法以在正确的元素上触发事件,或者在 ui.mouse 附加所需的处理程序时来回交换 this.element 值。这些以及我考虑过的任何其他方法看起来都非常困惑。
我看过原生的 jquery.resizable 小部件代码,它也创建了一个包装器,但据我所知,如果它试图触发事件,它也会遇到同样的问题。
这是我第一次使用 JQuery 小部件,所以我想向 JQuery 专家确认我没有遗漏任何东西?
最佳答案
我会尝试这样的方法:
_applyContainer: function ()
{
this.element.wrap("<span>");
this.element.bind("list of events", function(){
this.element.parent().trigger("name of event");
});
this.element = this.element.parent();
this.element.css({position:'relative'});
}
构建包装器元素时,绑定(bind)子元素,无论事件列表如何(如果这个概念不起作用,您可以迭代)并触发父(作用域,已知)元素同名事件。注意:“事件列表”应该类似于“mouseover mouseout click”等,然后“事件名称”应该替换为一些逻辑以检测哪些列表被触发。
关于javascript - JQuery UI 小部件 - 如何使用包装器元素引发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8894754/
我正在使用 SharePoint Online 并使用 Windows Azure 托管访问 SPO 的进程。 我们已将启动任务添加到 Azure 角色以安装 http://www.microsoft
我有一个函数,它获取包含时间的源文件(csv 文件),读取它,然后按顺序对行进行排序并将它们写入目标文件中。但是,如果源 csv 文件不存在,我需要引发 FileNotFoundError。我之前曾引
我试图在目录不存在时引发错误,然后再打开该目录中的文件。根据this response我应该为我的问题使用最具体的异常构造函数,我认为它是 NotADirectoryError。但是运行下面的代码我得
在编码/开发生命的一天或另一天,我们确实遇到了这个特殊的情况,这是最常见的异常(exception)之一。我的问题是关于的而不是。为什么(我知道当我们尝试访问实际上指向null的引用变量的属性时会引发
我想知道在 python 中是否可以在一个 except block 中引发异常并在稍后的 except block 中捕获它。我相信其他一些语言默认会这样做。 这是它的样子" try: som
我有以下代码: br = mechanize.Browser() br._factory.is_html = True br.form = mechanize._form.ParseString(''
我刚刚发现,如果您有一个引发 TOO_MANY_ROWS 异常的 SELECT INTO,该变量仍会从查询检索到的第一条记录中分配值。这是预期的行为吗? 这是我的例子: for co in my_cu
当 SSH 显示 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! 我知道当您重新安装远程服务器时会发生这种情况,但我尝试列出 其他原因 . 我知道如何
我有一个枚举和一个 EnumMap . 我将 map 放入一个类中以隐藏“字节”值。所以我有一个set(Parameter, int)和set(Parameter, boolean)方法。 publi
在什么情况下会redis-py引发以下 AttributeError 异常? redis-py 不是设计来引发仅基于 redis.exceptions.RedisError 的异常吗? 什么是合理的处
可悲的是,对此异常的引用通常具有异国情调,并且可能发生在您例如通过 Assembly.GetTypes() 枚举类型- 举个例子,它发生在我们的一个部署上,但同一组程序集在集成服务器上运行良好。 为了
我正在为 Android 下的特定平板电脑克隆一个存储库并获取源代码,我必须执行一个 python 脚本。当我执行它时,我收到此错误消息: Traceback (most recent call la
首先,执行此操作(在运行 4.4.2 的 Nexus 5 上测试): 将 PRIORITY_LOW 通知传递给 Service.startForeground()。 观察通知不显示在状态栏中。 使用相
我尝试使用 AppEngine 的 python 模块 api 来获取使用基本缩放的模块的实例数。在我模块的 yaml 文件中,我明确设置了 max_instances 参数。我希望 get_num_
当我如下运行我的 spark python 代码时: import pyspark conf = (pyspark.SparkConf() .setMaster("local")
在我的系统上,一段适用于 Python 2 的代码不适用于 Python 3。 f = open("plotwidget.svg") svgData = f.read() xml_stream = Q
我是 PHP 和 SQL 的新手,但我正在创建一个登录系统。我遇到的问题是: You have an error in your SQL syntax; check the manual that c
我有一个使用 ebaysdk 库的 python 代码,当我运行代码并输入关键字进行搜索时,我得到了这个错误。 Traceback (most recent call last): File "eba
当我将表单数据发送到我的 Flask 应用程序时,出现以下错误。它说它将使用 UTF-8 编码,但语言环境已经是 UTF-8。这个错误是什么意思? /home/.virtualenvs/project
在python2.7中,跟随pympler example : from anotherfile import somefunction, somecustomclass from os import
我是一名优秀的程序员,十分优秀!