Javascript: IE: outerHTML
-
19-09-2019 - |
Pergunta
problema que eu encontro com outerHTML usando navegador IE
Se eu disser:
txt="Update Complete!";
msg = sub.appendChild(d.createElement("p"));
msg.outerHTML = txt;
Ele funciona muito bem:
Mas se eu disser
txt="1 Error:<ul><li>Some Error</li></ul>";
msg = sub.appendChild(d.createElement("p"));
msg.outerHTML = txt;
me dá um erro de:
Message: Unknown runtime error
Line: 389
Char: 4
Code: 0
URI: http://10.1.1.16/schedule_calendar/js/validate.js
Linha: 389 pertence ao "msg.outerHTML = txt";
Alguém pode me ajudar com esta .. Obrigado antecipadamente
Solução
For some reasons I don't know, in most cases modifying outerHTML is not allowed. I am guessing that, this is because when you are modifying the outerHTML of an element you are actually replacing the current element.
IMO, it safer to replace a element than modifying its outerHTML.
In your case, maybe this will suffice:
UPDATES HERE:
<html>
<head><title>Test</title></head>
<body>
<div>
some content...
<div id="sub"></div>
</div>
<script type="text/javascript">
window.onload = function(){
var sub = document.getElementById('sub');
var txt="<p>Update Complete!</p>";
sub.innerHTML = txt;
alert('test 1');
txt="1 Error:<ul><li>Some Error</li></ul>";
sub.innerHTML = txt;
}
</script>
</body>
</html>
Outras dicas
What is your code trying to achieve? In both cases you do:
- create a new
p
element - append the
p
element tosub
(which I assume is an element itself as well) - set the
outerHTML
of thep
element to some text.
You could just as well have done:
- create a new textNode with the desired text data
append that to
sub
's childnodes:sub.appendChild(document.createTextNode("Update Complete!"));
Or, if sub
is empty anyway, simply do:
sub.innerHTML = "Update Complete!";