简体   繁体   English

我如何摆脱数千行

[英]How do I get rid of thousands of lines

To show which course a student is taking, I wrote these simple lines, click the student and see the course, but there seems to be thousands of code lines since I should list so many students and courses.为了显示学生正在学习哪门课程,我写了这些简单的行,单击学生并查看课程,但似乎有数千行代码,因为我应该列出这么多学生和课程。 Is there a way to write a single function to get the value of the object instead of writing a seperate function for each click?有没有办法编写单个 function 来获取 object 的值,而不是为每次点击编写单独的 function ?

 //and goes on thousands of times like this... var txt = document.getElementById("txt"); var students = { student1: "english", student2: "maths", student3: "history", student4: "geography", student5: "science", student6: "maths", student7: "maths", student8: "history", student9: "french", student10: "geography", }; //and there are thousands of students... function f1(){txt.innerHTML = students.student1;}; function f2(){txt.innerHTML = students.student2;}; function f3(){txt.innerHTML = students.student3;}; function f4(){txt.innerHTML = students.student4;}; function f5(){txt.innerHTML = students.student5;}; function f6(){txt.innerHTML = students.student6;}; function f7(){txt.innerHTML = students.student7;}; function f8(){txt.innerHTML = students.student8;}; function f9(){txt.innerHTML = students.student9;}; function f10(){txt.innerHTML = students.student10;}; // so there are thousands of functions...
 <a class="k" onClick="f1(); return false;" href="#">student1</a><br/> <a class="k" onClick="f2(); return false;" href="#">student2</a><br/> <a class="k" onClick="f3(); return false;" href="#">student3</a><br/> <div id="txt"></div>

Actually I tried this, reasoning that it would work:实际上我试过这个,理由是它会起作用:

var stdnt = document.querySelectorAll(".k);
var x = stdnt.innerHTML;
var result = "";

if ( x === students[x] ){
    x = students[x];
}
result = x;
function forAll (){
    txt.innerHTML = result;
}

But it doesn't work at all (I guess it's natural for an absolute beginner).但它根本不起作用(我想这对于绝对的初学者来说是很自然的)。 I badly need an idea.我非常需要一个想法。

You could condense it to use a single function and hand over innerHTML of the anchor elemenet.您可以将其压缩为使用单个 function 并移交锚元素的innerHTML This string can be used as accessor for the object.此字符串可用作 object 的访问器。

 var txt = document.getElementById("txt"); var students = { student1: "english", student2: "maths", student3: "history", student4: "geography", student5: "science", student6: "maths", student7: "maths", student8: "history", student9: "french", student10: "geography", }; function f(key) { txt.innerHTML = students[key]; };
 <a class="k" onClick="f(this.innerHTML); return false;" href="#">student1</a><br/> <a class="k" onClick="f(this.innerHTML); return false;" href="#">student2</a><br/> <a class="k" onClick="f(this.innerHTML); return false;" href="#">student3</a><br/> <div id="txt"></div>

Have a look at this看看这个

It is using several recommended methods它使用了几种推荐的方法

  1. eventListeners事件监听器
  2. DRY (Don't Repeat Yourself)干燥(不要重复自己)
  3. delegation代表团

 const students = { student1: "english", student2: "maths", student3: "history", student4: "geography", student5: "science", student6: "maths", student7: "maths", student8: "history", student9: "french", student10: "geography", }; // create the html so only one place to add a student const html = Object.entries(students).map(entry => `<a href="#" class="k" data-student="${entry[1]}">${entry[0]}</a>`); // on page load fill the student Div window.addEventListener('load', function() { const div = document.getElementById("studentDiv"); const text = document.getElementById('text') div.innerHTML = html.join('<br/>'); // delegate the click to the container, so only ONE event handler is used div.addEventListener('click', function(e) { const tgt = e.target; if (tgt.classList.contains('k')) { e.preventDefault(); // stop the link from executing text.innerHTML = tgt.dataset.student; } }); });
 <div id="studentDiv"></div> <div id="text"></div>

Simpler更简单

 const students = { student1: "english", student2: "maths", student3: "history", student4: "geography", student5: "science", student6: "maths", student7: "maths", student8: "history", student9: "french", student10: "geography", }; // create the html so only one place to add a student const html = Object.entries(students).map(entry => `<a href="#" class="k">${entry[0]}</a>`); // on page load fill the student Div window.addEventListener('load', function() { const div = document.getElementById("studentDiv"); const text = document.getElementById('text') div.innerHTML = html.join('<br/>'); // delegate the click to the container, so only ONE event handler is used div.addEventListener('click', function(e) { const tgt = e.target; if (tgt.classList.contains('k')) { e.preventDefault(); // stop the link from executing text.innerHTML = students[tgt.textContent] } }); });
 <div id="studentDiv"></div> <div id="text"></div>

You can make this in one function and maintaining the html for example, one to handle the id of the course:例如,您可以在一个 function 和维护 html 中进行此操作,以处理课程的 ID:

<a class="k"  onClick="getCourse(“student1”); return false;" href="#">student1</a><br/>


function getCourse(student) { // Contains the student
   txt.innerHTML = students[student];
 }

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

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