简体   繁体   English

JavaScript(jQuery)for循环问题

[英]JavaScript (jQuery) for-loop question

Hey all, I'm new to JavaScript and I'm using the jQuery library for this. 嘿,我是JavaScript新手,为此我正在使用jQuery库。 Basically I'm trying to create multiples of this line and I'm using ":eq(0) to do it. The issue is that :eq(0) repeats 3 times in the code and with the loop that I'm doing every time it repeats it has a different number. 基本上,我试图创建此行的倍数,并使用“:eq(0)来完成。问题是:: eq(0)在代码和我正在执行的循环中重复3次每次重复,它都有一个不同的数字。

This is what I'm getting from it i think (:eq(0), :eq(1),:eq(2), :eq(3), etc..) I need it to do this (:eq(0),:eq(0),:eq(0), :eq(1) :eq(1) :eq(1), etc...) 我认为这是我从中得到的(:eq(0)、: eq(1)、: eq(2)、: eq(3)等。)我需要它来做到这一点(:eq( 0),: eq(0),: eq(0),:eq(1):eq(1):eq(1)等)

for (i = 0; i < 6; ++i) {
    var $titleMarquee = '<marquee scrollamount="5" direction="left" width="233" align="left" behavior="alternate" loop="1"><span>';
    var $lieq = "li:eq("+i+")";
    $("ul.side-block-content "+$lieq+"").mouseenter(function() {
        $("ul.side-block-content "+$lieq+" .article-title a span")
            .replaceWith($titleMarquee+$("ul.side-block-content "+$lieq+" .article-title a").text()+"</span></marquee>");
    });
}

If anyone can let me know how to do this loop correctly, or maybe how to recreate the code for it to do the same thing that would be great. 如果有人可以让我知道如何正确执行此循环,或者可能如何为它重新创建代码以完成同样的事情,那将会很棒。

Thanks in advance. 提前致谢。

@Nick's answer: @尼克的答案:

var $titleMarquee = '<marquee scrollamount="5" direction="left" width="233" align="left" behavior="alternate" loop="1"><span>';
    for (i = 0; i < 6; ++i) {
        for (j = 0; j < 7; ++j) {
        $("ul.side-block-content li:eq("+i+")").mouseenter(function(){$("ul.side-block-content li:eq("+i+") .article-title a span").replaceWith($titleMarquee+$("ul.side-block-content li:eq("+i+") .article-title a").text()+"</span></marquee>");});
        $("ul.side-block-content li:eq("+i+")").mouseleave(function(){$("ul.side-block-content li:eq("+i+") .article-title a marquee").replaceWith('<span>'+$("ul.side-block-content li:eq("+i+") .article-title a").text()+"</span>");});  
        }
    }

This is what I'm using now and it's not working. 这是我现在正在使用的,并且无法正常工作。 Am I doing it correctly? 我做得对吗?

@Gilly3 @吉利3

$("ul.side-block-content li marquee").each(function() {
    this.stop();              // prevent the marquee from scrolling initially
    }).mouseenter(function() {
    this.start();             // start the scroll onmouseenter
    });

<marquee scrollamount="5" direction="left" width="233" align="left" behavior="alternate">

It looks like you are trying to make your <li> text scroll when you hover over it. 当您将鼠标悬停在<li>文本上时,您似乎正在尝试使其滚动。 Is that right? 那正确吗?

Just put the marquee code in the original html and do this: 只需将选取框代码放在原始html中,然后执行以下操作:

$(function ()
{
    $("ul.side-block-content li marquee").each(function() {
        this.stop();              // prevent the marquee from scrolling initially
    }).mouseenter(function() {
        this.start();             // start the scroll onmouseenter
    });
});

I also want to say not to use the marquee tag since it is deprecated and to use a jQuery plugin instead, but the last jQuery marquee plugin I saw was actually using a <marquee> in the back end anyway. 我也想说不要使用marquee标签,因为它已被弃用,而是使用jQuery插件,但是我看到的最后一个jQuery marquee插件实际上实际上是在后端使用<marquee> So... pfft. 所以...

You could embed another for loop inside, like so: 您可以在其中嵌入另一个for循环,如下所示:

for (i = 0; i < 6; ++i) {
  for (j = 0; j < 3; ++j) {
    // repeat i three times, and use :eq("+i+")
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM