在 javascript onbuttonclick 中定义函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4849683/
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
define function in javascript onbuttonclick
提问by Csabi
Hi How can I define a function on button click in HTML, I tried something like that:
嗨,我如何在 HTML 中定义按钮单击函数,我尝试了类似的方法:
<input type="button" value="sth" onclick="
<script type="text/javascript">
function hello()(
window.print();
var z =document.GetElementById('ogog');
z.inneHTML="hello";
<script>
">
I have to define my function here because I have code which generate me a site in which's header i can not define javascript function and I want one function on button click. this is only an example. This is the way it should look like?
我必须在这里定义我的函数,因为我有代码可以生成一个站点,其中的标题我无法定义 javascript 函数,我想要一个按钮单击函数。这只是一个例子。这是应该的样子吗?
采纳答案by epeleg
Assuming you realy want to definea new funciton on your onclick event you can try this:
假设你真的想在你的 onclick 事件上定义一个新的函数,你可以试试这个:
<input type="button" value="something" onclick="window.hello=function(){window.print();var z =document.GetElementById('ogog');z.innerHTML='hello';}">
回答by templatetypedef
You can attach a script to a button by putting a string representation of that script in the onclick handler:
您可以通过在 onclick 处理程序中放置该脚本的字符串表示来将脚本附加到按钮:
<input type="button" onclick="window.alert('Hi!')">
This code will be executed when the button is clicked. In general, the script tag is used to define scripts that can be referenced elsewhere in the HTML document, while strings containing JavaScript code are used in the body of the document to actually execute that code.
单击按钮时将执行此代码。通常,脚本标记用于定义可在 HTML 文档中的其他地方引用的脚本,而包含 JavaScript 代码的字符串则用于文档正文中以实际执行该代码。
回答by Lucius
You don't have to open the script tags, you just need to write directly the code you need.
You'd better include it inside a function defined in the head section, though:
您不必打开脚本标签,您只需要直接编写您需要的代码即可。
不过,您最好将其包含在 head 部分中定义的函数中:
<head>
<script type="text/javascript">
function hello () { ... }
</script>
</head>
<body>
<input type="button" onclick="hello()" />
</body>
or, even better, load it from an external .js file:
或者,更好的是,从外部 .js 文件加载它:
<script type="text/javascript" src="scripts.js"></script>
回答by Lloyd
You might want to try:
您可能想尝试:
<script type="text/javascript">
function hello() {
window.print();
var z =document.GetElementById('ogog');
z.inneHTML="hello";
}
</script>
<input type="button" value="sth" onclick="hello()"/>
回答by Sachin Shanbhag
You will have to define the script tag in the same file first, also '{'for function definitions -
您必须首先在同一个文件中定义脚本标签,还有'{'用于函数定义 -
<script type="text/javascript">
function hello() {
window.print();
var z =document.GetElementById('ogog');
z.inneHTML="hello";
}
</script>
Then call the function with its name -
然后用它的名字调用函数 -
<input type="button" value="sth" onclick="hello()"/>
Check this linkfor more details.
查看此链接了解更多详情。
回答by Juan Antonio
You can also put code directly, as for example a jQuery call, as for example onclick="$('#confirmation_modal').modal();"
to open a modal or onclick="$('#confirmation_modal').modal('toggle');"
to close it.
您也可以直接放置代码,例如 jQuery 调用,例如onclick="$('#confirmation_modal').modal();"
打开模态或onclick="$('#confirmation_modal').modal('toggle');"
关闭它。
Or also you can define a function and call it after.
或者你也可以定义一个函数并在之后调用它。
回答by aqllahdino
yyesThis code will be executed when the button is clicked. In general, the script tag is used to define scripts that can be referenced elsewhere in the HTML document, while strings containing JavaScript code are used in the body of the document to actually execute that code.
yyes 该代码会在按钮被点击时执行。通常,脚本标记用于定义可在 HTML 文档中的其他地方引用的脚本,而包含 JavaScript 代码的字符串则用于文档正文中以实际执行该代码。