简体   繁体   English

使用Javascript <li> onclick合并

[英]Javascript <li> onclick merged

I am attempting to load Case Studies through a clickable list of dynamically generated items. 我试图通过可动态生成的项目的可单击列表加载案例研究。 However my issue is, while it loads them properly, and if you click the elements, it loads a Case Study correctly, but it always loads the LAST case study assigned. 但是,我的问题是,尽管正确加载了这些元素,并且单击了元素,但它正确加载了一个案例研究,但始终加载了分配的LAST案例研究。

For example, if I have Case Study 1, Case Study 2 and Case Study 3, clicking any of the options takes me to Case Study 3. 例如,如果我有案例研究1,案例研究2和案例研究3,则单击任何选项都将带我进入案例研究3。

function MakeList(array){
//Create List Element
var list = document.createElement("ul");

//For each of the Array Elements
for(var i = 0; i < array.length; i++)
{
    //Create a ListItem Element
    window["listItem" + i] = document.createElement("li");
    eval("listItem" + i).setAttribute("id", ("listItem" + i));

    //And if it exists
    if(array[i] != undefined)
    {
        //Use the array elements title variable for the text node and localize the remaining object variables
        eval("listItem" + i).appendChild(document.createTextNode(array[i].title));
        var title = array[i].title;
        var icon = array[i].icon;
        var imageOne = array[i].imageOne;
        var imageTwo = array[i].imageTwo;
        var challenge = array[i].challenge;
        var solution = array[i].solution;
        var results = array[i].results;

        //Give the ListItem some Styling
        eval("listItem" + i).style.cursor = "pointer";
        eval("listItem" + i).style.color = "blue";
        eval("listItem" + i).style.margin = "0 0 1vh 0";

        //And add the onclick function to Load the selected CaseStudy
        eval("listItem" + i).onclick = function()
        {
            RemoveContent();
            LoadCaseStudy(title, icon, imageOne, imageTwo, challenge, solution, results);
        };

        //Add to the List
        list.appendChild(eval("listItem" + i));
    }
}

//Return the List
return list;}

I have tried giving them dynamically assigned IDs and variable names to seperate the on-click call, but no success. 我尝试给它们提供动态分配的ID和变量名以分隔单击呼叫,但没有成功。 Any advice? 有什么建议吗?

At that point in time when you eval the code in the onclick the FOR loop has already finished it's execution and "I" is at the .length - 1 of your array. 在那时,当您评估onclick中的代码时,FOR循环已经完成了执行,并且“ I”位于数组的.length-1处。

You should do something like this: 1. First declare your onclick handler outside of the code of the FOR loop 您应该执行以下操作:1.首先在FOR循环的代码之外声明onclick处理程序

function handler(title, icon, imageOne, imageTwo, challenge, solution, results)
    {
        RemoveContent();
        LoadCaseStudy(title, icon, imageOne, imageTwo, challenge, solution, results);
    }
  1. Attach the handler in a bit different way: 附加处理程序的方式有所不同:

    eval("listItem" + i).onclick = handler.bind(null, title, icon, imageOne, imageTwo, challenge, solution, results); eval(“ listItem” + i).onclick = handler.bind(空,标题,图标,imageOne,imageTwo,质询,解决方案,结果);

Where "null" can be an object representing your desired execution context. 其中“ null”可以是代表所需执行上下文的对象。

On another note Avoid using "EVAL" at all cost. 另一方面,请避免不惜一切代价使用“ EVAL”。 If you explain your case a bit better I will help you write the code without it. 如果您能更好地解释您的情况,我将帮助您在没有代码的情况下编写代码。 Give some HTML examples, or explain how the HTML is being built. 给出一些HTML示例,或解释如何构建HTML。

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

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