jQuery“这个”;遗留范围界定
我正在尝试向后移植我在 jQuery 1.6 中编写的幻灯片以在 Drupal 中工作,您可能知道也可能不知道它目前“锁定”到 jQuery 1.3(我知道,它很慢)。
不过,我在范围方面遇到了问题,因为 jQuery 1.3 的 $(this)
模型并不像更现代的版本中那样强大或有用。
以下是损坏的代码:
var $controls = $('a.controls', $frame);
$controls.click( function() {
var $clicked = $(this);
// ...
}
a.controls
表达式按预期生成 $controls
中两个对象的集合。但是当 jQuery 1.3 遇到 $(this)
赋值时,它会抛出以下错误:
this[0].ownerDocument 为空
有谁知道为什么要这样做,以及我如何修复或解决它?
I'm trying to back-port a slideshow I wrote in jQuery 1.6 to work in Drupal, which you may or may not know is "locked" to jQuery 1.3 at present (I know, it's retarded).
I'm having a problem with scope though, as jQuery 1.3's $(this)
model isn't as robust or helpful as it is in more modern releases.
Here's the code that's breaking:
var $controls = $('a.controls', $frame);
$controls.click( function() {
var $clicked = $(this);
// ...
}
The a.controls
expression results in a collection of two objects within $controls
as expected. But when jQuery 1.3 encouters the $(this)
assignment, it's throwing the following error:
this[0].ownerDocument is null
Does anyone know why it's doing this, and how I can fix or work around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能不是发生错误的地方,
this
上下文对于 1.3 中的事件回调的工作方式与最新版本中的相同。示例: http://jsfiddle.net/hPfZH/2/
$框架
上下文是否正确?$controls
上的点击事件会添加冲突吗?That’s probably not where the error is happening, the
this
context works the same for event callbacks in 1.3 as in the latest version.Example: http://jsfiddle.net/hPfZH/2/
$frame
context is correct?click events on the
$controls
that can add conflict?我认为完成此任务的唯一方法是对 jquery_update 模块进行一些小修改。您需要将sites/all/modules/jquery_update/replace中的jquery.min.js(或者jquery.js,如果您没有使用最小化版本)文件替换为最新的jQuery版本,然后对
jquery.module
文件(大约第 59 行):这基本上会告诉 jquery_update 仅替换非管理和非批处理页面上的 javascript,这应该可以满足您的目的。根据您的需要,您可能需要添加其他路径到该检查中。
我还没有测试过这个,但理论上它会起作用,不幸的是我无法评论 AHAH(Drupal 在 AJAX 框架上的蹩脚尝试)是否可以在非管理页面上与更高版本的 jQuery 一起工作。
希望你能解决这个问题,这是一个非常烦人的问题:-)
The only way I think this is going to get done is with a small hack of the jquery_update module. You need to replace the jquery.min.js (or jquery.js if you're not using the minimised version) file in sites/all/modules/jquery_update/replace with the latest jQuery version, then make the alterations to the
jquery.module
file (around line 59):That will basically tell jquery_update to only replace the javascript on non-admin and non-batch pages, which should serve your purpose. There may be other paths you need to add to that check depending on your needs.
I haven't tested this but in theory it will work, unfortunately I can't comment as to whether AHAH (Drupal's lame attempt at an AJAX framework) will work with the later version of jQuery on non-admin pages.
Hope you get this sorted it's a very annoying problem :-)
这可能无法回答问题,但可能会帮助您解决问题。
使用drupal模块jquery update,您可以在drupal中选择jquery版本。
This may not answer the question but may help you with the problem.
With drupal module jquery update you can choose the jquery version in drupal.