简体   繁体   English

在javascript中定义函数onbuttonclick

[英]define function in javascript onbuttonclick

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. 我必须在这里定义函数,因为我有生成我网站的代码,但我无法在其中定义javascript函数的标头,并且我希望单击按钮时有一个函数。 this is only an example. 这只是一个例子。 This is the way it should look like? 这应该是什么样子?

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. 通常,script标签用于定义可以在HTML文档的其他位置引用的脚本,而包含JavaScript代码的字符串用于文档正文中以实际执行该代码。

假设您确实想在onc​​lick事件上定义一个新功能,则可以尝试以下操作:

<input type="button" value="something" onclick="window.hello=function(){window.print();var z =document.GetElementById('ogog');z.innerHTML='hello';}">

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>

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()"/>

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 link for more details. 检查此链接以获取更多详细信息。

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. 通常,script标签用于定义可以在HTML文档的其他位置引用的脚本,而包含JavaScript代码的字符串用于文档正文中以实际执行该代码。

You can also put code directly, as for example a jQuery call, as for example onclick="$('#confirmation_modal').modal();" 您还可以直接放置代码,例如jQuery调用,例如onclick="$('#confirmation_modal').modal();" to open a modal or onclick="$('#confirmation_modal').modal('toggle');" 打开模式或onclick="$('#confirmation_modal').modal('toggle');" to close it. 关闭它。

Or also you can define a function and call it after. 或者,您也可以定义一个函数,然后再调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM