开发者

passing a local variable within the function

By clicking on the following DIV, nothing happens. Where is the e开发者_JS百科rror ?

<div onclick="function dummy(that) { alert(that.toString())}" class="next">></div>

Please help.


You are defining dummy but not calling it. I don't think it works that way, not in the HTML onclick property anyway.

I suggest you move dummy() into a separate code block:

<script type='text/javascript'>
function dummy(that) { alert(that.toString())}
</script>

and then:

<div onclick="dummy(this);" class="next">></div>

or attach the function programmatically like so:

document.getElementById("myDummyDIV").onclick = function(event) { ..... }


This should do the trick:

<div onclick="dummy(this);" class="next"></div>

<script type="text/javascript">
function dummy(that) {
    alert(that.toString());
}
</script>


This is silly actually. The function you've declared is unusable as a function unless you intend to do some more fantastic stuff and call the click event of this link from other methods elsewhere. However, if you're hell-bent-for-leather intent on putting the function declaration in the onclick event, it can be done this way:

<div onclick="(function dummy(that) { alert(that.toString())})();" class="next">></div>

You end up putting the function in it's own block and then the () at the end tells the parser to do it.


This is a function declaration, not invocation.

You could do something like this:

(function dummy(that) { alert(that.toString())}) (event);

and the complete HTML would be:

<div onclick="(function dummy(that) { alert(that.toString())})(event);" class="next">></div>


you dont create function here you can just write the following

<div onclick="alert(that.toString())" class="next">></div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜