Jump to content

JS DOM with IE error


elvis
 Share

Recommended Posts

Hi people, i was learning DOM in this morning, when I came a doubt, the script that I was writing did not work in IE. I'll put all HTML here:<html><head> <title>Teste DOM</title> <script language="javascript"> window.onload = function exibir(){ var fechar = document.createElement("a"); var fecharattr2 = document.createAttribute('href'); var fecharattr3 = document.createAttribute('onclick'); var nododetexto = document.createTextNode('Nodo de texto do javascript'); fecharattr2.value = '#'; fecharattr3.value = 'linkar();'; fechar.setAttributeNode(fecharattr2); fechar.setAttributeNode(fecharattr3); var pegar = document.getElementById('divpai'); pegar.appendChild(fechar); fechar.appendChild(nododetexto); } function linkar(){ var elemento = document.createElement('span'); var nodo = document.createTextNode('Texto de Nodo'); elemento.appendChild(nodo); var pegar = document.getElementById('divpai'); pegar.appendChild(elemento); } </script></head><body><div id="divpai"><br /></div><br /><a href="#" onclick="linkar()">Aparece</a></body></html>What must be do to solve this problem? Thanks

Link to comment
Share on other sites

Hi people, i was learning DOM in this morning, when I came a doubt, the script that I was writing did not work in IE. I'll put all HTML here:
What errors did IE give, or what should have happened but did not? I would rather not go to the trouble of testing your script if doing so is unnecessary.
Link to comment
Share on other sites

I think that's because JS doesn't access onclick handlers via the DOM. Try this instead:

fechar.onclick = linkar;

This is just a more direct way than specifying it in the HTML; an HTML onclick attribute would be translated to JS when the browser found it, but that's unnecessary with this approach.Also, you may be interested in this: http://developer.mozilla.org/en/docs/DOM:e...nt.setAttribute

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...