无需 onclick 即可运行 Javascript 功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19029816/
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
Run Javascript function without onclick
提问by SkSirius
I have a Javascript function for creating new form elements on a web page. The function is called by an onclick event.
我有一个用于在网页上创建新表单元素的 Javascript 函数。该函数由 onclick 事件调用。
I can't figure out how to run the function without the onclick event. I need to do this as I have Python code generating content to prepopulate the form elements, but still wish to be able add and remove the form elements dynamically with the Javascript.
我不知道如何在没有 onclick 事件的情况下运行该函数。我需要这样做,因为我有 Python 代码生成内容来预填充表单元素,但仍然希望能够使用 Javascript 动态添加和删除表单元素。
Javascript function:
Javascript函数:
pcount = 0;
createinputprice =function (){
field_area = document.getElementById('pricetable');
var tr = document.createElement("tr");
var cella = document.createElement("td");
var cellb = document.createElement("td");
var input = document.createElement("input");
var input2 = document.createElement("input");
input.id = 'descprice'+pcount;
input2.id = 'actprice'+pcount;
input.name = 'descprice'+pcount;
input2.name = 'actprice'+pcount;
input.type = "text";
input2.type = "text";
cella.appendChild(input);
cellb.appendChild(input2);
tr.appendChild(cella);
tr.appendChild(cellb);
field_area.appendChild(tr);
//create the removal link
var removalLink = document.createElement('a');
removalLink.onclick = function(){
this.parentNode.parentNode.removeChild(this.parentNode)
}
var removalText = document.createTextNode('Remove Field');
removalLink.appendChild(removalText);
tr.appendChild(removalLink);
pcount++
}
HTML:
HTML:
<table id="pricetable">
</table>
<a href='#' onclick="createinputprice()">Add Price</a>
<script type="text/javascript">
createinputprice();
</script>
The onclick event works fine in JSFiddle but calling the function directly doesn't work at all.
onclick 事件在 JSFiddle 中工作正常,但直接调用该函数根本不起作用。
回答by SkSirius
you can put it into tag using 'onload' event:
您可以使用 'onload' 事件将其放入标签中:
<body onload='yourfunction()'>
or just use jQuery:
或者只是使用jQuery:
$(document).ready(function() {
yourFunc();
});
回答by cptnk
I suppose you could use <body onload="yourfunction()">
you also should look into http://www.w3schools.com/js/js_htmldom_events.asp.
我想你可以使用<body onload="yourfunction()">
你也应该查看http://www.w3schools.com/js/js_htmldom_events.asp。
As you can see there are quite a few events available in javascript one should allways pick the right tool(event) for doing the work.
正如您所看到的,javascript 中有很多可用的事件,您应该始终选择正确的工具(事件)来完成工作。
回答by Ishan Jain
If you want to call jquery function on Page load: you can use $(document).ready(function(){});
如果你想在页面加载时调用 jquery 函数:你可以使用 $(document).ready(function(){});
For example:
例如:
Jquery-
jQuery-
<script type="text/javascript">
$(document).ready(function(){
createinputprice();
});
</script>
and you can also fire the click event in Jquery:
你也可以在 Jquery 中触发 click 事件:
HTML-
HTML-
<a id="myLink" href='#' onclick="createinputprice();">Add Price</a>
Jquery-
jQuery-
<script type="text/javascript">
$(document).ready(function(){
$('#myLink').click();
});
</script>