开发者

Why wont my appendChild work?

I have a little javascript app set up here: http://jsfiddle.net/faYMH/

What i want it to do is add a

<div><h1>Hi there and greetings!</h1></div>

after

<div id开发者_如何学编程='org_div1' onclick="addElement()">Hello</div>

using

<a href="#" onClick="addElement()" >add some</a>

(actually, what i want is for the onClick to go directly in the

So can anyone correct my code or provide some input?

(My next step is to also add a remove div, add an id to the new div with id + i++ )

Thanks so much!!


That's a quirk of JSFiddle. It wraps all of your code in a closure, so onclick handlers can't access your function. Either export the function into the global scope:

window.addElement=addElement;

Or change the little drop down in JSFiddle from "onLoad" to "no wrap (head)" or "no wrap (body)". While you're at it, you might want to change "Mootools" to "No-Library (pure JS)".


In your jsFiddle, addElement was scoped to inside the document ready handler and thus wasn't available to the click handler. I've modified the settings in the jsFiddle (without changing any code) to not have your code wrapped that way and it works for me now: http://jsfiddle.net/jfriend00/XVDYa/.

I changed the jsFiddle settings to "No-Library (pure JS)" and to "no wrap (head)". It's the second setting that really makes the difference here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜