简体   繁体   English

for循环4 DOM元素及其在JS中的事件

[英]for loop 4 DOM elements and their events in JS

I am trying to loop these 3 images followed by their ul and li. 我试图循环播放这3张图片,然后依次显示它们的ul和li。 Once clicked I came across an issue that the event handler isn't listening on any of the images other than the first. 单击后,我遇到了一个问题,即事件处理程序除了第一个以外,没有在其他任何图像上进行监听。 Please help me out. 请帮帮我。

Fiddle 小提琴

JS JS

var main = function () {

    var recipeArray = ['drink', 'firstCourse', 'mainDish', 'dessert'];
    var recipeArrayLength = recipeArray.length;
    for (var i = 0; i < recipeArrayLength; i++) {

        var $checkBoxes = document.createElement('div');
        $checkBoxes.setAttribute("class", "checkBoxes");
        var $recipePicContainer = document.createElement('span');
        $recipePicContainer.setAttribute("class", "recipePicContainer");


        function makeRecipePic() {
            var recipe = document.createElement("IMG");
            recipe.setAttribute("src", "../images/grasshopper-cocktail.jpg");
            recipe.setAttribute("class", "recipe");
            $recipePicContainer.appendChild(recipe);

        }

        makeRecipePic();

        var mainContainer = document.getElementsByClassName('mainContainer');
        mainContainer[0].appendChild($checkBoxes);

        var $div = document.createElement('div');
        $div.innerHTML = 'CockTails';
        $recipePicContainer.appendChild($div);
        $checkBoxes.appendChild($recipePicContainer);

        var $recipes = document.createElement("ul");
        var $checkboxes = document.getElementsByClassName('checkBoxes');

        var x = document.createElement("INPUT");
        x.setAttribute("type", "checkbox");
    }
        for (var j = 1; j <= 6; j++) {
            var li = document.createElement("li");
            li.className = "ingredients";
            li.setAttribute("type", "checkbox");


            var a = document.createElement("a");
            a.innerHTML = "Ingredient " + j;

            li.appendChild(a);
            $recipes.appendChild(li);
        }
        var b = document.createElement("li");
        li.innerHTML = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
        li.className = "Instructions";

        $checkboxes[0].appendChild($recipes);
        $('.recipes').hide();
        $('<input type="checkbox" value="1" />').prependTo(".ingredients");
        $('.mainContainer .recipePicContainer').click(function () {
            $(this).next('ul').toggle("slow").toggleClass('selected');

        });
};
$(document).ready(main);

You are only appending the ul and li elements to the first checkbox. 您只将ul和li元素附加到第一个复选框。 Therefore there is nothing to display when the onclick event is triggered for any of the other ones. 因此,当其他任何事件触发onclick事件时,将没有任何显示。

$checkboxes[0].appendChild($recipes);

You need to append children to the other checkboxes like you do here for the first checkbox in order for them to display something. 您需要像在此处为第一个复选框添加操作一样,将子级附加到其他复选框,以使它们显示内容。

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

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