JQuery Lint 说:“您多次使用同一个选择器。”我不认为我有
这是我的问题孩子:
jQuery(document).ready(function() {
var a = jQuery('img[title*="after"]');
a.parents('dl.gallery-item').addClass('after');
a.addClass('after');
var b = jQuery('img[title*="before"]');
b.parents('dl.gallery-item').addClass('before');
b.addClass('before');
//the following gives each image a 'name' attribute based on the caption, collapsed.
jQuery('img').each(function() {
var f = jQuery(this).parents('dl.gallery-item').find('dd').text();
var f = f.replace(/\s/g,'');
jQuery(this).attr('name', f);
});
//the following takes each 'name' attribute, finds the before, and sticks it behind the after
jQuery('img.after').hover(function() {
var imgName = jQuery(this).attr('name');
// alert(imgName);
var afterPosition_L = jQuery(this).offset().left;
var afterPosition_T = jQuery(this).offset().top;
var css_string = '{ top: '+ afterPosition_T +'; left: '+ afterPosition_L +'; position: absolute; z-index: 999;}';
var imgBefore = jQuery('img.before[name="'+ imgName +'"]');
// alert(imgBefore);
jQuery(imgBefore).css(css_string);
});
解释: 我正在使用 WordPress(所以我拼写出 jQuery)。 WordPress 在图库中加载一堆图像(使用 PHP)。根据“标题”的一些字段和标题的内容,我确定它是“之前”还是“之后”,并将该类分配给包装图像等的标签以及 img 本身。我还根据标题文本为图像分配了“名称”属性。所有这一切都运作良好。
“悬停”是事情变得糟糕的地方。
悬停时没有任何反应,但在鼠标移开时会引发 Lint 错误(在本文的标题中)。
应该发生什么: 首先,我获取悬停图像的名称并将其粘贴到 imgName 中(这是有效的)。 接下来,我得到悬停的 img 的位置,
这是丑陋的东西:我尝试使用“this”中的名称变量(即第一个)来识别图像 $('img.before[name=""]')我们悬停,并将其粘贴在变量“imgBefore”中,然后我尝试对其应用CSS,将其移动到与“after”相同的左侧/顶部位置,但具有不同的z-index。
'img.before[name=""] 似乎是 Lint 抱怨的选择器......它说:
Selector: "img.before[name="CarterLivingRoom"]"
You've used the same selector more than once.
所以它正确地使用了变量。它似乎确实在鼠标移出时抛出两次错误。也许“hover()”使用了它两次,但如何避免呢?
Here's my problem child:
jQuery(document).ready(function() {
var a = jQuery('img[title*="after"]');
a.parents('dl.gallery-item').addClass('after');
a.addClass('after');
var b = jQuery('img[title*="before"]');
b.parents('dl.gallery-item').addClass('before');
b.addClass('before');
//the following gives each image a 'name' attribute based on the caption, collapsed.
jQuery('img').each(function() {
var f = jQuery(this).parents('dl.gallery-item').find('dd').text();
var f = f.replace(/\s/g,'');
jQuery(this).attr('name', f);
});
//the following takes each 'name' attribute, finds the before, and sticks it behind the after
jQuery('img.after').hover(function() {
var imgName = jQuery(this).attr('name');
// alert(imgName);
var afterPosition_L = jQuery(this).offset().left;
var afterPosition_T = jQuery(this).offset().top;
var css_string = '{ top: '+ afterPosition_T +'; left: '+ afterPosition_L +'; position: absolute; z-index: 999;}';
var imgBefore = jQuery('img.before[name="'+ imgName +'"]');
// alert(imgBefore);
jQuery(imgBefore).css(css_string);
});
Explanation:
I'm working with WordPress (so I spell out jQuery). WordPress loads a bunch of images in a gallery (with PHP). Based on some fields for 'title' and the content of the caption, I determine if it's a 'before' or 'after' and assign that class both to a tag that wraps the image, et al and the img itself. I also assign a 'name' attribute to the image, based on the caption text. All this is working well.
The 'hover' is where things go bad.
When hovering nothing happens, but the Lint error (in the title of this post) is thrown on mouseout.
What's supposed to happen:
First, I get the name of the hovered image and stick it in imgName (this is working).
Next, I get the position of the hovered img
Here's the ugly stuff: I try to identify the image with $('img.before[name=""]') using the variable of the name from 'this', i.e. the one we're hovering, and stick it in a variable "imgBefore", then I try to apply CSS to it to move it to the same left/top position as the 'after' but with a different z-index.
The 'img.before[name=""] seems to be the selector that Lint is complaining about... it says:
Selector: "img.before[name="CarterLivingRoom"]"
You've used the same selector more than once.
So it is using the variable correctly. It does seem to throw the error twice on mouseout. Perhaps the 'hover()' is using it twice, but how can that be avoided?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里有很多问题。
document.ready
函数将 jQuery 对象作为参数传递,因此您可以在函数内使用$
而不必担心冲突。img.each
中,您多次重新定义var f
。var
一次是很好的形式;这也可以帮助您避免错误的重新声明。offset
对象;只需调用一次,然后使用结果对象的成员。imgBefore
) 中创建一个新的 jQuery 对象;没有必要这样做。此外,.css()
可以采用对象而不是字符串,这使得更新变得更加容易/干净。重构:
There are a number of things going wrong here.
document.ready
functions pass the jQuery object as a parameter, so you can use$
inside the function without fear of collision.img.each
, you redefinevar f
multiple times.var
once per function, at the top of the function; this helps you avoid mistaken redeclarations, as well.offset
objects; just call it once, then use the members of the resulting object.self
, so you can chain calls! This lets you clean up your code a lot.imgBefore
); no need to do that. Additionally,.css()
can take an object rather than a string, which makes updates a fair bit easier/cleaner.Refactored:
该错误消息可能具有误导性:。我看到的问题是:
jQuery(imgBefore)
是多余的。
imgBefore
已经是一个 jQuery 节点集。你可以只使用:The error message may be misleading:. The issue I see is that:
jQuery(imgBefore)
is redundant.
imgBefore
is already a jQuery node set. You can just use:我在这里看到的第一件事是你的最后一行是错误的:
它应该是:
First thing I see here is that your last line is wrong:
It should be:
昨晚我们找到了解决方案。正如 ifaour 和 Flaschen(谢谢!)指出的,一个问题是 imgBefore...我们还采取了其他几个步骤来纠正它。
我们最终得出这样的结论:
谢谢大家!
We found a solution late last night. One problem, as ifaour and Flaschen (thanks!) noted, was the imgBefore... we had a couple other steps to make it right.
We ended up with this:
Thank you all!