如何调用javascript函数onload和单击按钮? [英] how to called the javascript function onload and on button click?

查看:94
本文介绍了如何调用javascript函数onload和单击按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现此javascript被按钮调用
但是我希望它同时被加载,当单击按钮时,它也会调用该javscript
但我不确定如何?希望你们中的任何人都可以帮忙

i found this javascript is called by a button
but i want it to be onload at the same time , when button click it will call this javscript as well
but i not sure how? hopefully anyone of you can help out

function AddFileUpload()
         {
              var div = document.createElement('DIV');
              div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
              document.getElementById("FileUploadContainer").appendChild(div);
              counter++;
         }

推荐答案



尝试在body标签上使用onload事件的简单解决方案...
Hi,

Try this simple solution that is using onload event on body tag...
<html>
<head>
	<script>
		
	var counter = 0;

	function AddFileUpload()
	{
		var div = document.createElement('DIV');
		div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
		document.getElementById("FileUploadContainer").appendChild(div);
		counter++;
	}

	function RemoveFileUpload(obj)
	{
		return false;
	}
	
	</script>
</head>
<body onload="AddFileUpload();">
	<div id="FileUploadContainer"></div>
	<input type="button" value="Add" onclick="AddFileUpload();return false;">
</body>
</html></html>


如果您使用jquery,则可以使用.ready()事件来调用此dom DOM已准备就绪...


If you use jquery you can use .ready() event to call this whem dom is ready...

<html>
<head>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script>
		
	var counter = 0;

	function AddFileUpload()
	{
		var div = document.createElement('DIV');
		div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
		document.getElementById("FileUploadContainer").appendChild(div);
		counter++;
	}

	function RemoveFileUpload(obj)
	{
		return false;
	}


(文档).ready( function (){ AddFileUpload(); }); < / 脚本 > < /head > < 正文 > < div =" FileUploadContainer" < > < 输入 =" 按钮" 添加" onclick =" AddFileUpload();返回false;" < /body > < /html > < /html >
(document).ready(function () { AddFileUpload(); }); </script> </head> <body> <div id="FileUploadContainer"></div> <input type="button" value="Add" onclick="AddFileUpload();return false;"> </body> </html></html>


只需使用onClick = javascriptmethod();老兄,它将调用javascript
just use onClick=javascriptmethod(); Dude it will call the javascript


这篇关于如何调用javascript函数onload和单击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆