最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling Javascript function from a div - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Jan 27, 2013 at 20:38 user2002197user2002197 1011 gold badge3 silver badges7 bronze badges 1
  • What do you exactly mean by "when the following reference is made"? When the parser encounters the element with this ID? JavaScript is either executed when the parser encounters it in the HTML or as a reaction to some user interaction. What kind of interaction is "making a reference"? Your question really isn't clear. – Felix Kling Commented Jan 27, 2013 at 21:04
Add a ment  | 

1 Answer 1

Reset to default 3

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;
发布评论

评论列表(0)

  1. 暂无评论