简体   繁体   English

无法弄清楚为什么它不起作用了

[英]Can't figure out why it isn't working, anymore

So been trying to learn more javascript, by doing small projects that are simple but are starting stuff.所以一直试图通过做一些简单但开始的小项目来学习更多的 javascript。 One of the projects is a to-do app which for some people is really simple, but for me as a starter it's quite complex.其中一个项目是一个待办事项应用程序,对某些人来说非常简单,但对我作为初学者来说却相当复杂。

Now here is the thing, I had it working for the most part, I can add stuff, and one thing only HALF works, I wrote a bit that adds a X button to a li element.现在事情是这样的,我让它在大多数情况下工作,我可以添加东西,而且一件事只有 HALF 有效,我写了一点,将 X 按钮添加到 li 元素。 Now it works when I put the li element in the HTML page itself, but when it's added through javascript, it doesn't.现在当我将 li 元素放在 HTML 页面本身时它可以工作,但是当它通过 javascript 添加时,它不会。

There is no error, it was working before but for some reason it.. broke.没有错误,它以前可以工作,但由于某种原因它..坏了。

<!DOCTYPE html>
<head>
    <title>To Do App!</title>
    <link rel="stylesheet" type="text/css" href="CSS/stylesheet.css">
    
</head>
<body>
    <div id="h1Div">
        <h1> To-do app! </h1>
        <input type="text" id="inputForList">
        <input type="button" id="btnInput" value="Add me!" onclick="btnFunction()">
    </div>

    <ul id="ulSection">
        <li>Test 1</li>
        <li>Test 2</li>
    </ul>
    <script src="Scripts/javascript.js"></script>
</body>

This is the HTML page, super simple.这是HTML页面,超级简单。


//Adds li element with input from a textbox
function btnFunction(){
    var cLi = document.createElement("li");
    var inpList = document.getElementById("inputForList").value;
    var txtNode = document.createTextNode(inpList);
    cLi.appendChild(txtNode);

    //Check to see if anything is filled in, otherwise send message. And 'appends' it to the list item
    if(inpList === ''){
        alert("Voeg wat toe!");
    } else {
        document.getElementById("ulSection").appendChild(cLi);
    }
    // Reset value of Textbox to ""
    document.getElementById("inputForList").value = "";
}

//Sets a 'x' on every element.
var ulList = document.getElementsByTagName("li");
var i;
for(i = 0; i < ulList.length; i++){
    var span = document.createElement("span");
    var xBtn = document.createTextNode("\u00D7");
    span.className = "Done";
    span.appendChild(xBtn);

    ulList[i].appendChild(span);
}

And this is the Javascript.这就是 Javascript。

As stated, it worked before.如前所述,它以前有效。 But for some reason, now the bottom section, the X button (\×) part, it sn't working on the 'new stuff' that I add through the text input..但是出于某种原因,现在底部的 X 按钮 (\×) 部分无法处理我通过文本输入添加的“新内容”。

Your code is good, you just need to do the same thing you did in你的代码很好,你只需要做同样的事情

  var ulList = document.getElementsByTagName("li");
  var i;
  for (i = 0; i < ulList.length; i++) {
    var span = document.createElement("span");
    var xBtn = document.createTextNode("\u00D7");
    span.className = "Done";
    span.appendChild(xBtn);

    ulList[i].appendChild(span);
  }

in btnFunction() (with the exception of the for loop, which isn't needed in the function, as only one element is added at a time).btnFunction() (除了for循环,它在函数中不需要,因为一次只添加一个元素)。 The reason for this is your code only runs when the page is loaded, or when it is specifically told to run (in your case, on a button click).原因是您的代码仅在页面加载时运行,或者当它被明确告知运行时(在您的情况下,在单击按钮时)。 if you just create an element the js doesn't know to update it with an x , you have to tell it to do so.如果你只是创建一个元素,js 不知道用x更新它,你必须告诉它这样做。

 //Adds li element with input from a textbox function btnFunction() { var cLi = document.createElement("li"); var inpList = document.getElementById("inputForList").value; var txtNode = document.createTextNode(inpList); cLi.appendChild(txtNode); //Check to see if anything is filled in, otherwise send message. And 'appends' it to the list item if (inpList === '') { alert("Voeg wat toe!"); } else { document.getElementById("ulSection").appendChild(cLi); var ulList = document.getElementsByTagName("li"); var span = document.createElement("span"); var xBtn = document.createTextNode("\×"); span.className = "Done"; span.appendChild(xBtn); ulList[ulList.length-1].appendChild(span); } // Reset value of Textbox to "" document.getElementById("inputForList").value = ""; } //Sets a 'x' on every element. var ulList = document.getElementsByTagName("li"); var i; for (i = 0; i < ulList.length; i++) { var span = document.createElement("span"); var xBtn = document.createTextNode("\×"); span.className = "Done"; span.appendChild(xBtn); ulList[i].appendChild(span); }
 <!DOCTYPE html> <head> <title>To Do App!</title> <link rel="stylesheet" type="text/css" href="CSS/stylesheet.css"> </head> <body> <div id="h1Div"> <h1> To-do app! </h1> <input type="text" id="inputForList"> <input type="button" id="btnInput" value="Add me!" onclick="btnFunction()"> </div> <ul id="ulSection"> <li>Test 1</li> <li>Test 2</li> </ul> <script src="Scripts/javascript.js"></script> </body>

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

相关问题 无法弄清楚为什么JS插件无法正常工作 - Can't figure out why JS plugin isn't working 似乎无法弄清楚该脚本为何不起作用 - Can't seem to figure out why the script isn't working 我无法弄清楚为什么我添加事件处理程序的简单尝试无效 - I can't figure out why my simple attempt at adding an event handler isn't working 经典的元音练习。 我不知道为什么我的for循环无法正常工作 - classic vowel exercise. I can't figure out why my for loop isn't working 我不知道为什么我的石头,剪刀布游戏中的得分不起作用 - I can't figure out why the scoring isn't working in my rock, paper, scissors game 无法弄清楚为什么它不起作用。 javascript调用数据键 - Can't figure out why it isn't working. javascript calling data-key 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 无法弄清楚为什么javascript无法在我的php文件中工作 - Can't figure out why javascript isn't working in my php file 无法弄清楚为什么我的change(...)事件处理程序在这种情况下不起作用 - Can't figure out why my change(…) event handler isn't working in this scenario 无法弄清楚为什么setTimeout()无法正常运行 - can't figure out why setTimeout() isn't functioning properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM