如何使用 for 循环将重复的 javascript 片段打印到 HTML 文档?

自学业余爱好者试图创建一个工作表来帮助学生练习联立方程。我正在努力解决如何重复运行下面的代码以生成多个问题。

我认为问题出在[i]这里

document.getElementsByClassName("question")[i].getElementsByClassName("part")[n].innerHTML

有人可以向我解释为什么使用 for 循环变量像这样重复写入 HTML 不起作用以及如何修复它吗?

非常感谢您的帮助。

<div class="question">

  <ul>

    <li class="part"></li>

    <li class="part"></li>

    <li class="part"></li>

  </ul>

</div>


<div class="question">

  <ul>

    <li class="part"></li>

    <li class="part"></li>

    <li class="part"></li>

  </ul>

</div>




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

    var n = 12


    x = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))

    y = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))

    z = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))


    m = 20

    a = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    b = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    c = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))


    d = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    e = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    f = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))


    g = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    h = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    i = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))


    rhs1 = a*x + b*y + c*z

    rhs2=  d*x + e*y + f*z

    rhs3 = g*x + h*y + i*z


    document.getElementsByClassName("question")[i].getElementsByClassName("part")[0].innerHTML= a + " x + " + b + " y + " + z + " z = " + rhs1;

    document.getElementsByClassName("question")[i].getElementsByClassName("part")[1].innerHTML= d + " x + " + e + " y + " + f + " z = " + rhs2;

    document.getElementsByClassName("question")[i].getElementsByClassName("part")[2].innerHTML= g + " x + " + h + " y + " + i + " z = " + rhs3;


  }


米脂
浏览 125回答 4
4回答

海绵宝宝撒

您遇到的问题是您i在 for 循环中引用了增量值,但随后将其值更改为计算值。我把它改为cntr.for (cntr=0; cntr<5; cntr++){var n = 12&nbsp; &nbsp; x = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))&nbsp; &nbsp; y = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))&nbsp; &nbsp; z = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))&nbsp; &nbsp; m = 20&nbsp; &nbsp; a = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; b = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; c = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; d = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; e = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; f = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; g = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; h = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; i = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))&nbsp; &nbsp; rhs1 = a*x + b*y + c*z&nbsp; &nbsp; rhs2=&nbsp; d*x + e*y + f*z&nbsp; &nbsp; rhs3 = g*x + h*y + i*z&nbsp; &nbsp; document.getElementsByClassName("question")[cntr].getElementsByClassName("part")[0].innerHTML= a + " x + " + b + " y + " + z + " z = " + rhs1;&nbsp; &nbsp; document.getElementsByClassName("question")[cntr].getElementsByClassName("part")[1].innerHTML= d + " x + " + e + " y + " + f + " z = " + rhs2;&nbsp; &nbsp; document.getElementsByClassName("question")[cntr].getElementsByClassName("part")[2].innerHTML= g + " x + " + h + " y + " + i + " z = " + rhs3;&nbsp; }

犯罪嫌疑人X

试试这个,第一次children[0]访问ul元素,第二次children[0 or 1 or 2]访问部分,也稍作更改,您可以设置一个等于 document.getElementsByClassName("question") 的变量,这样您就不需要每次都请求它questions&nbsp;=&nbsp;document.getElementsByClassName("question") questions[i].children[0].children[0].innerHTML&nbsp;=&nbsp;a&nbsp;+&nbsp;"&nbsp;x&nbsp;+&nbsp;"&nbsp;+&nbsp;b&nbsp;+&nbsp;"&nbsp;y&nbsp;+&nbsp;"&nbsp;+&nbsp;z&nbsp;+&nbsp;"&nbsp;z&nbsp;=&nbsp;"&nbsp;+&nbsp;rhs1; questions[i].children[0].children[1].innerHTML&nbsp;=&nbsp;d&nbsp;+&nbsp;"&nbsp;x&nbsp;+&nbsp;"&nbsp;+&nbsp;e&nbsp;+&nbsp;"&nbsp;y&nbsp;+&nbsp;"&nbsp;+&nbsp;f&nbsp;+&nbsp;"&nbsp;z&nbsp;=&nbsp;"&nbsp;+&nbsp;rhs2; questions[i].children[0].children[2].innerHTML&nbsp;=&nbsp;g&nbsp;+&nbsp;"&nbsp;x&nbsp;+&nbsp;"&nbsp;+&nbsp;h&nbsp;+&nbsp;"&nbsp;y&nbsp;+&nbsp;"&nbsp;+&nbsp;i&nbsp;+&nbsp;"&nbsp;z&nbsp;=&nbsp;"&nbsp;+&nbsp;rhs3;

慕桂英546537

提取函数:function getTriplet(n) {&nbsp; return [0, 1, 2].map(_ => (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n)));}const [x, y, z] = getTriplet(12);console.log(x, y, z);动态生成 DOM:function getQuestion(parts) {&nbsp; const q = document.createElement('div');&nbsp; q.classList.add('question');&nbsp; const list = document.createElement('ul');&nbsp; for (const part of parts) {&nbsp; &nbsp; const item = document.createElement('li');&nbsp; &nbsp; item.classList.add('part');&nbsp; &nbsp; item.innerText = part;&nbsp; &nbsp; list.appendChild(item);&nbsp; }&nbsp; q.appendChild(list);&nbsp; return q;}document.body.appendChild(getQuestion(['A', 'B', 'C']));document.body.appendChild(getQuestion(['1', '2', '3']));使用字符串模板插值以获得更清晰的效果:const a = 6;const b = 7;document.querySelector('sample').innerText = `${a} * ${b} = ${a * b}`;<sample></sample>不要使用全局变量。例如,您可以重用全局 i 进行循环和计算。

芜湖不芜

也许你应该研究以下结构://<![CDATA[/* js/external.js */let doc, htm, bod, nav, M, I, mobile, S, Q, rand, signedRand; // for use on other loadsaddEventListener('load', ()=>{doc = document; htm = doc.documentElement; bod = doc.body; nav = navigator; M = tag=>doc.createElement(tag); I = id=>doc.getElementById(id);mobile = nav.userAgent.match(/Mobi/i) ? true : false;S = (selector, within)=>{&nbsp; var w = within || doc;&nbsp; return w.querySelector(selector);}Q = (selector, within)=>{&nbsp; var w = within || doc;&nbsp; return w.querySelectorAll(selector);}rand = (min, max)=>{&nbsp; let mn = min, mx = max;&nbsp; if(mx === undefined){&nbsp; &nbsp; mx = mn; mn = 0;&nbsp; }&nbsp; return mn+Math.floor(Math.random()*(mx-mn+1));}signedRand = (min, max)=>{&nbsp; return (Math.random() < 0.5 ? 1 : -1)*rand(min, max);}// magic under here - you could put below on a separate page using a load event - execept the end loadconst question = Q('.question');let n, x, y, z, a, b, c, d, e, f, g, h, i, xyz, p;for(let q of question){&nbsp; n = 13; x = signedRand(n); y = signedRand(n); z = signedRand(n);&nbsp; n = 21; a = signedRand(n); b = signedRand(n); c = signedRand(n);&nbsp; d = signedRand(n); e = signedRand(n); f = signedRand(n);&nbsp; g = signedRand(n); h = signedRand(n); i = signedRand(n);&nbsp; xyz = S('.xyz', q); p = Q('.part', q);&nbsp; xyz.textContent = 'x = '+x+'; y = '+y+'; z = '+z+';';&nbsp; p[0].textContent = a+'x + '+b+'y + '+c+'z = '+(a*x+b*y+c*z);&nbsp; p[1].textContent = d+'x + '+e+'y + '+f+'z = '+(d*x+e*y+f*z);&nbsp; p[2].textContent = g+'x + '+h+'y + '+i+'z = '+(g*x+h*y+i*z);}}); // end load//]]>/* css/external.css */*{&nbsp; box-sizing:border-box; font-size:0; color:#000; padding:0; margin:0; overflow:hidden;}html,body,.main{&nbsp; width:100%; height:100%;}.main{&nbsp; background:#333; overflow-y:auto; padding:7px 10px;}.question>div{&nbsp; color:#fff; font:bold 22px Tahoma, Geneva, sans-serif;}.question>.xyz{&nbsp; background:#ccc; color:#000; padding:3px 5px; margin-bottom:2px; border-radius:1px;}.question{&nbsp; padding:7px; border:1px solid #fff; border-radius:2px; margin-bottom:7px;}<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>&nbsp; <head>&nbsp; &nbsp; <meta charset='UTF-8' /><meta name='viewport' content='width=device-width, height=device-height, initial-scale:1, user-scalable=no' />&nbsp; &nbsp; <title>Title Here</title>&nbsp; &nbsp; <link type='text/css' rel='stylesheet' href='css/external.css' />&nbsp; &nbsp; <script src='js/external.js'></script>&nbsp; </head><body>&nbsp; <div class='main'>&nbsp; &nbsp; <div class='question'>&nbsp; &nbsp; &nbsp; <div class='xyz'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class='question'>&nbsp; &nbsp; &nbsp; <div class='xyz'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class='question'>&nbsp; &nbsp; &nbsp; <div class='xyz'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class='question'>&nbsp; &nbsp; &nbsp; <div class='xyz'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class='question'>&nbsp; &nbsp; &nbsp; <div class='xyz'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class='question'>&nbsp; &nbsp; &nbsp; <div class='xyz'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class='question'>&nbsp; &nbsp; &nbsp; <div class='xyz'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; &nbsp; <div class='part'></div>&nbsp; &nbsp; </div>&nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript