prevAll 的 JQuery 选择器问题
我无法让一些 Jquery 代码正常工作。我认为我的问题源于我如何编写选择器。
这是我的原始 HTML:
<div class="rotate">
<div class="offer-holder"><span class="lblSeconds">7</span></div>
<div class="offer-holder"><span class="lblSeconds">3</span></div></div>
...这是我的 Jquery 代码:
$("div.rotate div.offer-holder").hide().eq(1).show();
function rotate() {
var i = $("div.rotate div.offer-holder:visible").prevAll("div").length + 1;
i = i % $("div.rotate div.offer-holder").length;
var speed = $("div.rotate div.offer-holder").fadeOut().eq(i).fadeIn().find('.lblSeconds').text();
setTimeout("rotate()", (parseInt(speed) * 1000));
};
rotate();
这工作得很好(顺便说一句,在 CSS 中,offer-holder 的位置设置为绝对,顶部:0px,左侧:0px)。顺便说一句,这是一个广告旋转器。所有的 div 相互重叠,然后一一出现,不断循环。
现在,我必须修改另一个页面,它看起来像这样:
<div class="rotate">
<div class="ob-offer-item rounded-corners clearfix">
<div class="test">
<div class="ob-offer-details">
<div class="offer-holder"> XXX </div></div></div></div></div>
...这是我尝试过的:
$("div.rotate div.ob-offer-item div.test div.ob-offer-details div.offer-holder").hide().eq(1).show();
function rotate() {
var i = $("div.rotate div.ob-offer-item div.test d div.ob-offer-details div.offer-holder:visible").prevAll("div").length + 1;
i = i % $("div.rotate div.ob-offer-item div.test d div.ob-offer-details div.offer-holder").length;
var speed = $("div.rotate div.ob-offer-item div.test d div.ob-offer-details div.offer-holder").fadeOut().eq(i).fadeIn().find('.lblSeconds').text();
setTimeout("rotate()", (parseInt(speed) * 1000));
};
rotate();
我怀疑我的问题是 prevAll。它基本上隐藏了它之前的每个 div(我猜,这就是我告诉它要做的事情)。它不是根据 lblSeconds 值从一个广告到下一个广告的漂亮淡入淡出,而是每秒连续闪烁。我不知道修复 prevAll 选择器路径的正确语法。我尝试了几种组合,但没有成功。
欢迎任何帮助。
谢谢。
斯蒂芬
I'm having trouble getting some Jquery code to work. I think my problem is stems around how I'm writing the selector.
Here's my original HTML:
<div class="rotate">
<div class="offer-holder"><span class="lblSeconds">7</span></div>
<div class="offer-holder"><span class="lblSeconds">3</span></div></div>
...and here's my Jquery code:
$("div.rotate div.offer-holder").hide().eq(1).show();
function rotate() {
var i = $("div.rotate div.offer-holder:visible").prevAll("div").length + 1;
i = i % $("div.rotate div.offer-holder").length;
var speed = $("div.rotate div.offer-holder").fadeOut().eq(i).fadeIn().find('.lblSeconds').text();
setTimeout("rotate()", (parseInt(speed) * 1000));
};
rotate();
This works perfectly (incidently, in the CSS, the position of offer-holder is set to absolute, top: 0px, left: 0px). BTW, this is an ad rotator. All of the divs overlap each other, then appear one-by-one, looping continuously.
Now, I have to modify it for another page, which looks something like this:
<div class="rotate">
<div class="ob-offer-item rounded-corners clearfix">
<div class="test">
<div class="ob-offer-details">
<div class="offer-holder"> XXX </div></div></div></div></div>
...and here's what I tried:
$("div.rotate div.ob-offer-item div.test div.ob-offer-details div.offer-holder").hide().eq(1).show();
function rotate() {
var i = $("div.rotate div.ob-offer-item div.test d div.ob-offer-details div.offer-holder:visible").prevAll("div").length + 1;
i = i % $("div.rotate div.ob-offer-item div.test d div.ob-offer-details div.offer-holder").length;
var speed = $("div.rotate div.ob-offer-item div.test d div.ob-offer-details div.offer-holder").fadeOut().eq(i).fadeIn().find('.lblSeconds').text();
setTimeout("rotate()", (parseInt(speed) * 1000));
};
rotate();
I suspect my problem is the prevAll. It's basically hiding every div before it (which, I guess, is what I'm telling it to do). Instead of a nice fade from one ad to the next based on the lblSeconds value, it flickers continuously every second. I don't know the correct syntax to fix the prevAll selector path. I tried several combinations, to no avail.
Any assistance is welcome.
Thanks.
Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是 JSfiddle http://jsfiddle.net/pdV39/ 与您的第一种情况。您在 setTimeout 调用中出现错误...它应该是:
而不是
您的选择器也不完全正确:
没有找到任何内容。您不需要包含您尝试查找的每个图层,除非存在类冲突并且您需要更精确。所以这里是对原始 jsfiddle 的更新,它确实有效并且应该回答您的问题。 http://jsfiddle.net/pdV39/1/
Okay here's a JSfiddle http://jsfiddle.net/pdV39/ with your first situation. You had an error in the setTimeout call...it should be:
instead of
Your selectors aren't completely correct either:
Doesn't find anything. You don't need to include every single layer you're trying to find unless there is a collision of classes and you need to be more precise. So here's an update to the original jsfiddle that does work and should answer your question. http://jsfiddle.net/pdV39/1/