tinfanide Posted July 2, 2011 Share Posted July 2, 2011 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script>window.onload = function(){ // div1 var div1 = document.getElementById('div1'); var a = document.createElement('a'); var txt = document.createTextNode('This is a link'); a.appendChild(txt); div1.appendChild(a); a.setAttribute('href','http://www.google.com/');// div2 var div2 = document.getElementById('div2'); div2.innerHTML = "<a href='http://www.google.com/'>Google</a>";// div3 var newdiv = document.createElement('div'); newdiv.id = 'div3'; document.body.appendChild(newdiv); div3 = document.getElementById('div3'); div3.innerHTML = "div3"; //IE does not show it up }</script></head><body><div id="div1"></div><div id="div2"></div></body></html> Div1 & Div2 work perfectly well in both IE and Firefox.But how about Div3 where I used createElement and .id of JS?It works in Firefox but not IE.Is there anything wrong with my script? Link to comment Share on other sites More sharing options...
jeffman Posted July 2, 2011 Share Posted July 2, 2011 1. When you first declare a variable (when you use it the first time) use the var keyword.2. Older versions of IE did not work correctly if the name of a variable was the same as the id of an element. Try naming the variable something else. Link to comment Share on other sites More sharing options...
tinfanide Posted July 2, 2011 Author Share Posted July 2, 2011 1. When you first declare a variable (when you use it the first time) use the var keyword.2. Older versions of IE did not work correctly if the name of a variable was the same as the id of an element. Try naming the variable something else.Yes, good for the remark.My IE8 cannot recognise a first-declared variable without "var" but still works well when it reads a variable with the same name to the id. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.