- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的问题 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);
});
说明:我正在使用 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()”使用了它两次,但如何避免呢?
最佳答案
这里有很多问题。
document.ready
函数将 jQuery 对象作为参数传递,因此您可以在函数内使用 $
而不必担心冲突。img.each
中,您多次重新定义 var f
。var
一次是一种良好的形式;这也可以帮助您避免错误的重新声明。offset
对象;只需调用一次,然后使用结果对象的成员。self
,因此您可以链式调用!这可以让您大量清理代码。imgBefore
) 中创建一个新的 jQuery 对象;没有必要这样做。此外,.css()
可以采用对象而不是字符串,这使得更新变得更加容易/干净。重构:
jQuery(document).ready(function($) {
$('img[title*="after"]').addClass('after').
parents('dl.gallery-item').addClass('after');
$('img[title*="before"]').addClass('before').
parents('dl.gallery-item').addClass('before');
//the following gives each image a 'name' attribute based on the caption, collapsed.
$('img').each(function() {
var $this = $(this), f;
f = $this.parents('dl.gallery-item').find('dd').text().replace(/\s/g, '');
$this.attr('name', f);
});
//the following takes each 'name' attribute, finds the before, and sticks it behind the after
$('img.after').hover(function() {
var $this = $(this), offset = $this.offset();
$('img.before[name="' + $this.attr('name') + '"]').css({
top: offset.top,
left: offset.left,
position: 'absolute',
'z-index': 999
});
});
});
关于JQuery Lint 说 : "You' ve used the same selector more than once ."I don' t think I have,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4559023/
我想知道这两者之间有什么不同 .myClass/DomElement .myotherclassinsidethatelement 和 .myClass/DomElement > .myothercl
使用 jQuery on() 版本 1.7。我通常这样绑定(bind)我的事件: $(".foo").on("click", function() { console.log("foo cli
我想找到与选择器匹配的所有元素,但如果它已经包含在匹配元素中则不查找。 $('#container').find('.child').not('.child .child'); 请注意,.child
我有一个看起来像这样的无序列表,但更广泛: Parent Category 2 Parent Category 2 Parent Category 3
这个问题在这里已经有了答案: CSS negation pseudo-class :not() for parent/ancestor elements (2 个答案) 关闭 4 年前。
我希望使用 CSS :not() 来定位 before 选择器。这可能吗? 示例: https://jsfiddle.net/uuq62b8d/ a.button:before { content
这有什么区别: $.each($('#myTable input[name="deleteItem[]"]:checked').do_something()); 还有这个: $('#myTable i
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在使用 UL LI 列表和 jQuery 创建一棵树。我使用了 jQuery 选择器 jQuery(li:has(ul)) 查找所有具有子节点的列表节点,然后向其添加单击事件。 jQuery(li
我真的不知道如何命名这两种方法,所以请原谅我这样调用它们。 字符串选择器 $("#myList li").eq(3); 函数选择器 $("#myList li:eq(3)"); 据我所知,他们都做同样
我有以下代码: .. 我正在使用以下 CSS 来排除具有“main-l tbl”类的表: table:not(.main-l .views-table) { .. } 我注
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: What is the difference between $ and jQuery 我注意到使用“jQuery(
我有许多 css 选择器和许多选择器异常,所以我使用 :not 将它们排除在外... 示例(只是一些我不需要的选择器): [class*="-dashboard-"]:not([class$="-bi
CADisplayLink 有这个方法是有道理的,但我很好奇为什么 UIScreen 也会有它。 最佳答案 文档说屏幕提供的显示链接与该屏幕相关联。但是,查看官方文档,与任何屏幕都没有明显的关系;显示
我在这里阅读了关于 toArray() 的文档,并在控制台中对其进行了测试。我找不到在选择器上调用 toArray() 和调用选择器本身之间的区别。 两种方式都得到了完全相同的结果,这是一个与选择器匹
我有一个问题,为什么这两个代码片段不同。 $('#ctl00_DDMenu1_HyperLink1') //jQuery(a#ctl00_DDMenu1_HyperLink1 Default.asp
我想通过以下方式模拟我可以在 jQuery 中实现的目标$('.someClass:not(.hidden)') 我试过下面的代码。 $crawler->filter('someClass:not(.
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我想通过以下方式模拟我可以在 jQuery 中实现的目标$('.someClass:not(.hidden)') 我试过下面的代码。 $crawler->filter('someClass:not(.
我想根据 Iterator::next 中当前枚举变体的某些属性更改枚举变体。我有两次尝试,都没有编译: enum Test { A(Vec), B, } impl Iterator
我是一名优秀的程序员,十分优秀!