从div调用Javascript函数 [英] Calling Javascript function from a div

查看:129
本文介绍了从div调用Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的函数:

我可以请一些帮助来调用在Javascript中创建的函数, / p>

 函数testFunction()
{
alert(Test);
}

我希望在HTML中创建以下引用时调用testFunction :

 < div id =testFunction> 

我可以请一些帮助吗?



在标记中:


=h2_lin>解决方案


 < div id =testFunctiononclick =testFunction()> 

或者在脚本块内:

  function testFunction(){
alert(Test);
}

var el = document.getElementById(testFunction);
el.onclick = testFunction;


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.

Here is my function:

        function testFunction()
        {
            alert("Test");
        }

I would like the testFunction to be called when the following reference is made in HTML:

<div id="testFunction"> 

Can I please have some help to do this?

解决方案

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;

这篇关于从div调用Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆