简体   繁体   English

document.getElementById无法使用,如何解决?

[英]document.getElementById won’t work, how do I fix this?

I have this site that converts a quadratic equation to the vertex form. 我有一个将二次方程转换为顶点形式的站点。

What I did was add a form with inputs where you can add in the variables required to convert the equation. 我所做的就是添加带有输入的表单,您可以在其中添加转换方程式所需的变量。 I am almost done with it, but when I try to call getElementById with the IDs h and k , the document can't because it was defined after the user hits the submit button, therefore I cannot load the function on startup which would have solved my problem. 我已经差不多完成了,但是当我尝试用ID hk调用getElementById时,该文档无法执行,因为它是在用户单击Submit按钮后定义的,因此无法在启动时加载该函数,而该函数已经解决了我的问题。

 function abc(a, b, c) { var a_el = document.getElementById('a'); var b_el = document.getElementById('b'); var c_el = document.getElementById('c'); var av = a_el.value; var bv = b_el.value; var cv = c_el.value; console.log(av); console.log(bv); console.log(cv); var h = (-1 * bv) / (2 * av); console.log(h); var k = (av * (Math.pow(h, 2))) + (bv * h) + cv; console.log(k); document.getElementById("av").innerHTML = av; document.getElementById("bv").innerHTML = bv; document.getElementById("cv").innerHTML = cv; document.getElementById("h").innerHTML = h; document.getElementById("k").innerHTML = k; } 
 <form> <div class="form-group"> <label>a:</label> <input type="text" class="form-control" id="a"> </div> <div class="form-group"> <label>b:</label> <input type="text" class="form-control" id="b"> </div> <div class="form-group"> <label>c:</label> <input type="text" class="form-control" id="c"> </div> <input type="button" class="btn btn-default" name="submit" value="Submit" onclick="abc();"></input> </form> <math xmlns="http://www.w3.org/1998/Math/MathML"> <mi>f</mi> <mo>(</mo> <mi>x</mi> <mo>)</mo> <mo>=</mo> <mn id="av"></mn> <msup> <mrow> <mo>(</mo> <mi>x</mi> <mo>-</mo> <mn id="h"></mn> <mo>)</mo> </mrow> <mrow> <mn>2</mn> </mrow> </msup> <mo>+</mo> <mn id="k"></mn> <math> </div> <div class="pull-right"> <img src="quad.png" class="img-rounded pull-right thumbnail"> </div> </div> </body> 

It was not working in the code snippet mostly because some of the tags with IDs were not defined and also because the JavaScript is automatically enclosed inside a self executing anonymous function (function(){})() in the code snippet in Stack Overflow thus the abc function was undefined when the onclick handler was added. 它在代码段中不起作用,主要是因为未定义某些ID的标签,并且还因为JavaScript被自动封装在Stack Overflow的代码段的自执行匿名函数(function(){})() 。添加onclick处理程序时, abc函数未定义。 Thus adding a click event listener in JavaScript resolved this issue as well. 因此,在JavaScript中添加click事件侦听器也可以解决此问题。 This was solely to make it run in the code snippet in Stack Overflow. 这仅仅是为了使其在Stack Overflow的代码段中运行。

You can delegate the onclick event or you could embed the script inside a <script> tag and add it to the HTML. 您可以委派onclick事件,也可以将脚本嵌入<script>标记内并将其添加到HTML。

 (function(){ function abc() { var a_el = document.getElementById('a'); var b_el = document.getElementById('b'); var c_el = document.getElementById('c'); var av = a_el.value; var bv = b_el.value; var cv = c_el.value; console.log(av); console.log(bv); console.log(cv); var h = (-1 * bv) / (2 * av); console.log(h); var k = (av * (Math.pow(h, 2))) + (bv * h) + cv; console.log(k); document.getElementById("av").innerHTML = av; document.getElementById("h").innerHTML = h; document.getElementById("k").innerHTML = k; } document.getElementById("submit").addEventListener("click",function(){ abc(); }); })(); 
 <form> <div class="form-group"> <label>a:</label> <input type="text" class="form-control" id="a"> </div> <div class="form-group"> <label>b:</label> <input type="text" class="form-control" id="b"> </div> <div class="form-group"> <label>c:</label> <input type="text" class="form-control" id="c"> </div> <input type="button" class="btn btn-default" name="submit" value="Submit" id="submit"></input> </form> <math xmlns="http://www.w3.org/1998/Math/MathML"> <mi>f</mi> <mo>(</mo> <mi>x</mi> <mo>)</mo> <mo>=</mo> <mn id="av"></mn> <msup> <mrow> <mo>(</mo> <mi>x</mi> <mo>-</mo> <mn id="h"></mn> <mo>)</mo> </mrow> <mrow> <mn>2</mn> </mrow> </msup> <mo>+</mo> <mi id="k"></mi> <math> </div> <div class="pull-right"> <img src="quad.png" class="img-rounded pull-right thumbnail"> </div> </div> </body> 

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

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