admin管理员组

文章数量:1024625

The document.createElement('a') tag is not working in IE8 in this snippet of code. It works fine in chrome and firefox. When I check through IE, it seems as if its the first line of code that's broken.

        item = document.createElement('a');
        item.setAttribute('id', 'memorize');
        item.innerHTML = "<?php echo wzfactory::get_xml('menu_item', 4, $); ?>";
        item.setAttribute('class', 'menu_button');
        item.onclick = function() {redirect('memorizor', 'memorize', 0);};
        menu_div.appendChild(item);

Does anybody know why this is the case, and if there is a crappy browser workaround? I haven't been able to find the workaround.

The document.createElement('a') tag is not working in IE8 in this snippet of code. It works fine in chrome and firefox. When I check through IE, it seems as if its the first line of code that's broken.

        item = document.createElement('a');
        item.setAttribute('id', 'memorize');
        item.innerHTML = "<?php echo wzfactory::get_xml('menu_item', 4, $); ?>";
        item.setAttribute('class', 'menu_button');
        item.onclick = function() {redirect('memorizor', 'memorize', 0);};
        menu_div.appendChild(item);

Does anybody know why this is the case, and if there is a crappy browser workaround? I haven't been able to find the workaround.

Share Improve this question edited Aug 9, 2017 at 7:30 FelixSFD 6,10210 gold badges46 silver badges130 bronze badges asked Sep 11, 2011 at 10:52 DaveDave 11 gold badge1 silver badge1 bronze badge 4
  • Wele to SO. Please always quote the exact error message you are getting. Also, seeing as it's a JS question, it would be better if you showed the final generated HTML/JS instead of the PHP source – Pekka Commented Sep 11, 2011 at 10:54
  • 1 How have you determined that createElement isn't working? – Michael Mior Commented Sep 11, 2011 at 10:55
  • Broken how? What errors do you get? What debugging tools are you using? What makes you think the problem is with the first line of code? Why are you showing us PHP that generates JavaScript instead of the JavaScript itself? Why are you using setAttribute which is known to be broken in many versions of IE (especially for setting class)? – Quentin Commented Sep 11, 2011 at 10:56
  • Possible duplicate of createElement error in IE8 – FelixSFD Commented Aug 9, 2017 at 7:30
Add a ment  | 

1 Answer 1

Reset to default 2

Couple of things that might "break" the functionality.. try this instead:

item = document.createElement('a');
item.id = 'memorize';
item.href = "#";
item.innerHTML = "<?php echo wzfactory::get_xml('menu_item', 4, $); ?>";
item.className 'menu_button';
item.onclick = function() {
    redirect('memorizor', 'memorize', 0);
    return false;
};
menu_div.appendChild(item);

First, assign the id directly and not via setAttribute and second, anchor without href bees mere text, not link so you must assign that attribute.

The document.createElement('a') tag is not working in IE8 in this snippet of code. It works fine in chrome and firefox. When I check through IE, it seems as if its the first line of code that's broken.

        item = document.createElement('a');
        item.setAttribute('id', 'memorize');
        item.innerHTML = "<?php echo wzfactory::get_xml('menu_item', 4, $); ?>";
        item.setAttribute('class', 'menu_button');
        item.onclick = function() {redirect('memorizor', 'memorize', 0);};
        menu_div.appendChild(item);

Does anybody know why this is the case, and if there is a crappy browser workaround? I haven't been able to find the workaround.

The document.createElement('a') tag is not working in IE8 in this snippet of code. It works fine in chrome and firefox. When I check through IE, it seems as if its the first line of code that's broken.

        item = document.createElement('a');
        item.setAttribute('id', 'memorize');
        item.innerHTML = "<?php echo wzfactory::get_xml('menu_item', 4, $); ?>";
        item.setAttribute('class', 'menu_button');
        item.onclick = function() {redirect('memorizor', 'memorize', 0);};
        menu_div.appendChild(item);

Does anybody know why this is the case, and if there is a crappy browser workaround? I haven't been able to find the workaround.

Share Improve this question edited Aug 9, 2017 at 7:30 FelixSFD 6,10210 gold badges46 silver badges130 bronze badges asked Sep 11, 2011 at 10:52 DaveDave 11 gold badge1 silver badge1 bronze badge 4
  • Wele to SO. Please always quote the exact error message you are getting. Also, seeing as it's a JS question, it would be better if you showed the final generated HTML/JS instead of the PHP source – Pekka Commented Sep 11, 2011 at 10:54
  • 1 How have you determined that createElement isn't working? – Michael Mior Commented Sep 11, 2011 at 10:55
  • Broken how? What errors do you get? What debugging tools are you using? What makes you think the problem is with the first line of code? Why are you showing us PHP that generates JavaScript instead of the JavaScript itself? Why are you using setAttribute which is known to be broken in many versions of IE (especially for setting class)? – Quentin Commented Sep 11, 2011 at 10:56
  • Possible duplicate of createElement error in IE8 – FelixSFD Commented Aug 9, 2017 at 7:30
Add a ment  | 

1 Answer 1

Reset to default 2

Couple of things that might "break" the functionality.. try this instead:

item = document.createElement('a');
item.id = 'memorize';
item.href = "#";
item.innerHTML = "<?php echo wzfactory::get_xml('menu_item', 4, $); ?>";
item.className 'menu_button';
item.onclick = function() {
    redirect('memorizor', 'memorize', 0);
    return false;
};
menu_div.appendChild(item);

First, assign the id directly and not via setAttribute and second, anchor without href bees mere text, not link so you must assign that attribute.

本文标签: javascriptdocumentcreateElement is not working in IE8Stack Overflow