在javascript onbuttonclick中定义函数

Csa*_*abi 5 html javascript onclick

嗨如何在HTML中按钮单击定义一个函数,我尝试过这样的事情:

<input type="button" value="sth" onclick="
<script type="text/javascript">
function hello()(
window.print();
var z =document.GetElementById('ogog');
z.inneHTML="hello";
<script>
">
Run Code Online (Sandbox Code Playgroud)

我必须在这里定义我的函数,因为我有代码生成一个网站,其中的标题我无法定义javascript函数,我想要一个按钮单击功能.这只是一个例子.这应该是什么样子?

tem*_*def 6

您可以通过将该脚本的字符串表示形式放在onclick处理程序中来将脚本附加到按钮:

<input type="button" onclick="window.alert('Hi!')">
Run Code Online (Sandbox Code Playgroud)

单击按钮时将执行此代码.通常,脚本标记用于定义可在HTML文档中的其他位置引用的脚本,而包含JavaScript代码的字符串则在文档正文中用于实际执行该代码.


epe*_*leg 5

假设你真的想在你的 onclick 事件上定义一个新的函数,你可以试试这个:

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