从 div 调用 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14551992/
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
Calling Javascript function from a div
提问by user2002197
Can I please have some help to call a function that is created in Javascript when a reference is made to a DIV in HTML.
当引用 HTML 中的 DIV 时,我能否帮助调用在 Javascript 中创建的函数。
Here is my function:
这是我的功能:
function testFunction()
{
alert("Test");
}
I would like the testFunction to be called when the following reference is made in HTML:
我希望在 HTML 中进行以下引用时调用 testFunction:
<div id="testFunction">
Can I please have some help to do this?
我可以请一些帮助来做到这一点吗?
采纳答案by Konstantin Dinev
You can attach the call to a click handler:
您可以将调用附加到点击处理程序:
In markup:
在标记中:
<div id="testFunction" onclick="testFunction()">
Or inside your script block:
或在您的脚本块内:
function testFunction() {
alert("Test");
}
var el = document.getElementById("testFunction");
el.onclick = testFunction;

