简体   繁体   English

Javascript + jquery,在for循环内运行

[英]Javascript + jquery, function inside a for loop

Basically i've got a lot of repetition in my code as im changing a few bits and pieces inside my function to accomodate the different divs such as class name, text and padding. 基本上,我在代码中有很多重复之处,因为我在函数内部更改了一些零碎的部分以适应不同的div,例如类名,文本和填充。 Rather then writing them all out individually like so; 而不是像这样单独写出所有内容;

$('.about').hover(function () {
        $('.clickable7').text("Find out about us").css('padding-top', '137px').fadeIn(200);
        $('.positional').css("background-image", "none");
    },
    function () {
        $('.clickable7').fadeOut(200);
        $('.positional').css('background-image', 'url(assets/imgs/prototype6.png)');
    });
$('.friends').hover(function () {
        $('.clickable7').text("Meet our Friends").css('padding-top', '137px').fadeIn(200);
        $('.positional').css("background-image", "none");
    },
    function () {
        $('.clickable7').fadeOut(200);
        $('.positional').css('background-image', 'url(assets/imgs/prototype6.png)');
    });

I'm trying to write a function that will do it for me, i've gotten the functionality down but it kills the rest of my javascript code for some reason. 我正在尝试编写一个可以为我完成的功能,我已经取消了该功能,但是由于某种原因它杀死了我的其余JavaScript代码。

var titles = [
    {
        name: ".about",
        text: "find out about us",
        padding: 137
    },
    {
        name: ".friends",
        text: "meet our friends",
        padding: 137
    }
];

for (i = 0; i <= 6; i++) {
    (function(index) {
        var name = titles[index].name;
        var text = titles[index].text;
        var padding = titles[index].padding;

        $(name).hover(function () {
            $('.clickable7').text(text).css('padding-top', padding).fadeIn(200);
            $('.positional').css("background-image", "none");
        },
        function () {
            $('.clickable7').fadeOut(200);
            $('.positional').css('background-image', 'url(assets/imgs/prototype6.png)');
        });
    })(i);
};

Any help will be greatly appreciated :) 任何帮助将不胜感激 :)

edit--- my html 编辑---我的HTML

<div class="positional">
<a href="#about">
    <div class="about click">About</div>
</a>
<a href="#friends">
    <div class="friends click">Friends</div>
</a>
<a href="#">
    <div class="purpose click">Purpose</div>
</a>
<a href="#">
    <div class="empowerment click">Empowerment</div>
</a>
<a href="#contact">
    <div class="contact click">Contact Us</div>
</a>
<a href="#">
    <div class="passion click">Passion</div>
</a>

<div class="clickable7"></div>

Upon inspection of the loop termination condition, I suggest changing 检查环路终止条件后,建议更改

for (i = 0; i <= 6; i++) { 

to

for (i = 0; i < titles.length; i++) {

because it looks like you have 6 links, but you set your for-loop to terminate at 6, so titles[6] will cause an OOB error which probably prevents the rest of your code from running. 因为看起来您有6个链接,但是将for循环设置为在6处终止,所以titles[6]将导致OOB错误,这可能会阻止其余代码运行。


It's also not advisable to put anonymous functions in a loop if you can help it. 如果可以帮助的话,也不建议将匿名函数放入循环中。

I took the liberty of making another way to accomplish your task without using loops. 我采取了另一种不使用循环即可完成任务的方式。 Basically, you can put data into the divs and use just one function to utilize the data and populate your hidden div. 基本上,您可以将数据放入div中,而仅使用一个函数来利用数据并填充隐藏的div。 There are other ways, but this is just one humble solution. 还有其他方法,但这只是一个谦虚的解决方案。

Oh yes, you can use .stop() to prevent all the animation from chaining up between hovers. 哦,是的,您可以使用.stop()防止所有动画在悬停之间链接。

 $('.click').hover(function() { $this = $(this); $('.clickable7') .text($this.data('text')) .css('padding-top', $this.data('padding')) .stop() .fadeIn(200); }, function() { $('.clickable7') .stop() .fadeOut(200); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="positional"> <a href="#about"> <div class="about click" data-text="Find out about us" data-padding="37">About</div> </a> <a href="#friends"> <div class="friends click" data-text="Meet our Friends" data-padding="57">Friends</div> </a> <a href="#"> <div class="purpose click" data-text="See our purpose" data-padding="97">Purpose</div> </a> <a href="#"> <div class="empowerment click" data-text="Be empowered!" data-padding="17">Empowerment</div> </a> <a href="#contact"> <div class="contact click" data-text="Contact us" data-padding="69">Contact Us</div> </a> <a href="#"> <div class="passion click" data-text="Yay, passion!" data-padding="88">Passion</div> </a> <div class="clickable7"></div> 

The for loop is terminated with a ; for循环以;终止; which is a wrong syntax. 这是错误的语法。 As Drakes mentioned, defining anonymous functions inside a loop should be avoided as this usually creates closure issues. 如Drakes所述,应避免在循环内定义匿名函数,因为这通常会导致关闭问题。 I have extracted the anonymous function like this: 我已经提取了匿名函数,如下所示:

(function($){
    var i,
        titles = [
        {
            name: ".about",
            text: "find out about us",
            padding: 137
        },
        {
            name: ".friends",
            text: "meet our friends",
            padding: 137
        }
    ];

    function bindHoverHandlers(index){
        var name = titles[index].name,
            text = titles[index].text,
            padding = titles[index].padding;

        $(name).hover(function () {
             $('.clickable7').text(text).css('padding-top', padding).fadeIn(200);
             $('.positional').css("background-image", "none");
         }, function () {
             $('.clickable7').fadeOut(200);
             $('.positional').css('background-image', 'url(assets/imgs/prototype6.png)');
         });
    }

    for (i = 0; i < titles.length; i++) {
        bindHoverHandlers(i);
    }
}(window.jQuery));

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

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