简体   繁体   中英

define function in javascript onbuttonclick

Hi How can I define a function on button click in HTML, I tried something like that:

<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?

You can attach a script to a button by putting a string representation of that script in the onclick handler:

<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.

假设您确实想在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>
<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:

<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.

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.

Or also you can define a function and call it after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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