
Originally Posted by
Kcirtap
d.style.background-color = #00ffff ;
That should fail. The string needs to be quoted and the property name should either be in "camel" notation, or use the setProperty method:
Code:
d.style.backgroundColor = '#00ffff';
d.style.setProperty('background-color', '#00ffff', '');
The colour code could also be changed to either '#0ff' or 'aqua'.
d.write("blablabla") ; [...]
Should that work or is there something close, or am i totally out of the blue?
No. NN4 has something like that, but NN4 is (or should be) dead and the technique ended with it.
To modify the content of a document after it has loaded requires a totally new approach and there are, in fact, two: the document object model (DOM) and the proprietary innerHTML property.
The DOM allows the creation, insertion, and removable of both elements and text. It also allows groups of elements to be moved from one part of the document to another. Whilst it is very flexible and powerful, it isn't good at adding large amounts of new content - it just gets messy. Arguably, this is because adding large amounts of new content isn't something one should be doing on the Web as it reduces accessibility and introduces a reliance on scripting.
The second approach, innerHTML, allows a script to write an arbitrary string containing HTML mark-up into an element. It's simpler to use, though it tends to be slower, particularly on Gecko-based browsers.
Which you use depends on the modifications you're attempting to make. If it's just plain text or a small amount of HTML, I'd use the DOM. If it's more complicated, use innerHTML.
Using innerHTML is just like using any other string property:
Code:
d.innerHTML = 'blah blah blah';
That would overwrite the content of d with the given text. If you need to insert or append content, just treat the innerHTML property as any other string variable and use the string manipulation methods like substring, indexOf, and the concatenation operators (+ and +=).
Using the DOM is more complicated, so I'll leave that for now. Ask if you're interested.
The question behind all this is, I have methods that apply to objects like
document for example. Can I not create an object of type document that can take document's methods?
No. The document object is a host object which is pre-defined. It represents the entire document so you can't simply construct a new instance. Even if you did, there would be no reasonable relationship with a div element (and the object that represents it) so applying document methods would be meaningless.
Hope that helps,
Mike
Bookmarks