javascript 无需单击任何内容即可调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15159676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a function without clicking anything
提问by Yngve Vahit
I'm new in programming and I'm trying to call a function without any button or click event. I'm doing a table within a table using javascript function. Here's my code so far:
我是编程新手,我试图在没有任何按钮或点击事件的情况下调用一个函数。我正在使用 javascript 函数在表中创建一个表。到目前为止,这是我的代码:
<html>
<head> <title> Hello </title> </head>
<body>
<table border=1>
<tr>
<td> Hello! <input type='hidden' value='0' id='theValue' /> <script> add(); </script> <div id='myDiv'> </div> </td>
</tr>
<script>
$ctra = 1;
$ctr1a = 1;
function add() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = "<table border=1>"+
"<tr>"+
"<td> Hello! <input type='hidden' value='0' id='theValue' /> <script> add(); </script> <div id=('" + divIdName + "')> </div> </td>"+
"</tr>"+
"</table>";
if($ctra<100){
ni.appendChild(newdiv);
$ctra++;
}
}
</script>
</table>
</body>
</html>
When I run it, it displays
当我运行它时,它显示
"+ ""+ "
Hello!
"; if($ctra<100){ ni.appendChild(newdiv); $ctra++; } }
in the browser. What could the problem be? Thank you in advance!
EDIT
在浏览器中。可能是什么问题?先感谢您!
编辑
function add() {
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = "<table border=1><tr><td> Hello! <input type='hidden' value=0 id='theValue' /><div id='" + divIdName + "'></td></tr></table>";
ni.appendChild(newdiv);
for(var i=1;i<100;i++) {
var ni = document.getElementById(divIdName);
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = "<table border=1><tr><td> Hello! <input type='hidden' value='" + i + "' id='theValue' /><div id='" + divIdName + "'></td></tr></table>";
ni.appendChild(newdiv);
}
}
回答by Sachin
Try this
试试这个
<html>
<head> <title> Hello </title> </head>
<script type="text/javascript">
$ctra = 1;
$ctr1a = 1;
function add() {
alert('s')
var ni = document.getElementById('myDiv');
var numi = document.getElementById('theValue');
var num = (document.getElementById('theValue').value -1)+ 2;
numi.value = num;
var newdiv = document.createElement('div');
var divIdName = 'my'+num+'Div';
newdiv.setAttribute('id',divIdName);
newdiv.innerHTML = "<table border=1><tr><td> Hello! <input type='hidden' value='0' id='theValue' /></tr></table>";
if($ctra<100){
ni.appendChild(newdiv);
$ctra++;
}
}
</script>
<body onload="add()">
<table border="1">
<tr>
<td> Hello! <input type='hidden' value='0' id='theValue' />
<div id='myDiv'> </div> </td>
</tr>
</table>
</body>
</html>
Hope it works for you..:)
希望对你有帮助..:)
回答by MrMinshall
Your JS results in nested blocks, this isn't allowed in html. The function add() in your blocks is not defined, because when the browser encounters it, it hasn't seen your definition of add() yet. Your table contains a script that contains another table which contains yet another script. It's very confusing :P
您的 JS 导致嵌套块,这在 html 中是不允许的。块中的函数 add() 没有定义,因为当浏览器遇到它时,它还没有看到你对 add() 的定义。您的表包含一个脚本,该脚本包含另一个包含另一个脚本的表。这非常令人困惑:P
OK, if i understand correctly: you have a single table. You'd like to have JS put another table inside. The thing you want to put inside is, in this example, a copy of the table. You want to end up with a table inside a table and you want to achieve it programatically.
好的,如果我理解正确:你有一张桌子。你想让 JS 在里面放另一张桌子。在本例中,您想要放入的内容是表的副本。您希望在表中包含一个表,并且希望以编程方式实现它。
First separate your concerns: scripts and html: If you put your add() after the definition and you put your script after the body then the browser will encounter it and run it. HTH:
首先将您的关注点分开:脚本和 html:如果您将 add() 放在定义之后,并将脚本放在正文之后,那么浏览器将遇到它并运行它。HTH:
<html>
<head> <title> Hello </title> </head>
<body>
<table id="t1" border=1>
<tr>
<td> Hello! <input type='hidden' value='0' id='theValue' />
<div id='myDiv'></div>
</td>
</tr>
</table>
<script>
function add_table() {
var table1 = document.getElementById("t1"),
table2 = table1.cloneNode(true),
mydiv = document.getElementById("myDiv"),
mydiv2;
table2.setAttribute('id', 't2');
mydiv2 = table2.getElementsByTagName("div")[0];
mydiv2.setAttribute('id', 'myDiv2');
mydiv.appendChild(table2);
}
// call the function
add_table();
</script>
</body>
</html>
回答by leafnode
You have </script>
inside the code. Browser interprets that as an end of javascript and breaks back to HTML. To fix this, you can divide this string into for example </sc'+'ript>
. That will fix this issue, but probably you'll still have to work a bit on the script, but that's a different story.
你有</script>
里面的代码。浏览器将其解释为 javascript 的结束并返回到 HTML。要解决此问题,您可以将此字符串拆分为例如</sc'+'ript>
. 这将解决这个问题,但可能你仍然需要在脚本上做一些工作,但那是另一回事了。
回答by Menencia
You have to switch between differents quotes, and remove braces for the id, like this :
您必须在不同的引号之间切换,并删除 id 的大括号,如下所示:
newdiv.innerHTML = "<table border=1>"
+"<tr>"
+'<td> Hello! <input type="hidden" value="0" id="theValue" /> '
+'<script> add(); </script> <div id="' + divIdName + '"> </div> </td>'
+"</tr>"
+"</table>";
回答by Deepu
Because you cant access the function without any evnt in the dom..You have to call the function on any event of the elemnt.
因为你不能在dom中没有任何evnt的情况下访问该函数..你必须在元素的任何事件上调用该函数。
回答by mindandmedia
- move the script outside the table-element to the head or to the end of the body.
- first add the div, then call the script. you might want to call your
add()
in<body onload='add'>
- remove the braces here to create valid html:
<div id=('" + divIdName + "')>
- 将表元素外的脚本移动到头部或正文的末尾。
- 首先添加div,然后调用脚本。你可能想打电话给你
add()
的<body onload='add'>
- 删除此处的大括号以创建有效的 html:
<div id=('" + divIdName + "')>