RobB wrote:
[...]
Did you want the HTML to be parsed, or discarded? If it's the former,
this might be a textbook case for the value of innerHTML....
http://www.developer-x.com/content/innerhtml/
Or a textbook case for ensuring that the text object(s) were
appropriately serialized in the XML in the first place. ;-)
(I make no admission to knowing what that format might be!)
Which requires the obvious response: supposing I wanted to use DOM to
create text nodes for the above case, what is the appropriate format
so that I can create appropriate DOM objects then give them
appropriate attributes?
Below is a feeble attempt to get a start on it, but I could not work
out how to read styles from an object then apply them to an element.
e.g. if I create a span 'oSpan' and put some text into it with a text
node, then create say w='color' and y='red', the following does
not seem to work:
oSpan = document.createElement('span');
oSpan.appendChild(document.createTextNode('stuff') );
oSpan.style.w = y;
Then append oSpan to the document, 'stuff' is in the default
color. If I explicitly use:
oSpan.style.color = 'red';
or
oSpan.style.color = y;
'stuff' is red.
If there really is no way to do this? Do I need a case statement for
every single style attribute I want to support? If so, why isn't
innerHTML (or some similar method) available in DOM 3? Or is there
and I'm just displaying my ignorance?
<style type="text/css">
..rg {color:red;}
</style>
<script type="text/javascript">
function addStuff(z){
var tgt = document.getElementById(z);
// obj should be created by parsing XML, save that for later...
var obj = {};
obj[0] = {text:'this ','color':'red'}
obj[1] = {text:'is','fontWeight':'bold'}
obj[2] = {text:' html','fontWeight':'normal'}
var i = 0;
var oTxt, oSpan;
do {
for (w in obj[i]){
switch (w) {
case 'text':
alert('Creating: ' + w + ' \'' + obj[i][w] + '\'');
oSpan = document.createElement('span');
oTxt = document.createTextNode(obj[i][w]);
oSpan.appendChild(oTxt);
break;
default:
alert('Setting style: ' + w + ' to \'' + obj[i][w] + '\'');
// oSpan.style.w = obj[i][w];
// oSpan.style.w = '\'' + obj[i][w] + '\'';
// oSpan.style.fontWeight = 'bold';
oSpan.style.color = obj[i][w];
}
}
alert('adding ' + oSpan.nodeName);
tgt.appendChild(oSpan);
}
while ( obj[++i] )
}
</script>
<input type="button" onclick="addStuff('xx')" value="addStuff">
<br>
<span id="xx"></span><br>
<span class="rg">sample text</span>
--
Rob