[英]Clicking button element in function, JavaScript
I have a problem with button in my code.我的代码中的按钮有问题。 I created two functions: szescian() and tabela().我创建了两个函数:szescian() 和 tabela()。 szescian() is loading after clicking on picture and tabela() should be loading when clicking a button (with function szescian() is creating) by this code:单击图片后 szescian() 正在加载,并且在单击按钮时应加载 tabela()(使用 szescian() 函数正在创建)通过以下代码:
document.getElementById("przycisk").onclick = tabela();
, but when I'm clicking this image both functions are loading at the same time. ,但是当我点击这张图片时,这两个功能同时加载。 What's the problem?有什么问题?
var br = document.createElement("br");
var input = document.createElement("input");
function szescian(){
var wynik = document.getElementById("wynik");
var txt = document.createTextNode("Wpisz długość boku a:" );
wynik.appendChild(txt);
wynik.appendChild(br);
input.type = "text";
wynik.appendChild(input);
var button = document.createElement("input");
button.type = "button";
wynik.appendChild(button);
button.setAttribute("id", "przycisk");
button.value = "Oblicz";
document.getElementById("przycisk").onclick = tabela();
}
var a=input.value;
var pol=a*a*a;
var obj=a;
function tabela(){
var wynik = document.getElementById("wynik");
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
....
You should give the function reference , not the return value.您应该提供函数引用,而不是返回值。 Remove the ()
:删除()
:
document.getElementById("przycisk").onclick = tabela;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.