elvis Posted July 26, 2008 Share Posted July 26, 2008 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 More sharing options...
Jesdisciple Posted July 26, 2008 Share Posted July 26, 2008 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 More sharing options...
elvis Posted July 26, 2008 Author Share Posted July 26, 2008 Sorry, in IE, the Attrubute "onclick" created in "exibir()" function does not work. I do not know why. Link to comment Share on other sites More sharing options...
Jesdisciple Posted July 26, 2008 Share Posted July 26, 2008 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 More sharing options...
elvis Posted July 26, 2008 Author Share Posted July 26, 2008 It works! Very very thanks Jesdiciple! I am beginning to learn javascript now, and you tip about setattribute will be very useful. Really thank you. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.