"Fabian" <la****@hotmail.com> writes:
Fabian hu kiteb:
Ho can I create a function in javascript to dynamically toggle all
<em> tags between bold and italic?
Specifically, without adding name or id attributes to these tags, is it
possible to find all <em> (or whatever) tags?
Yes. In standard compliant browsers, and IE from version 5, you can use
Setting all ems italic:
var ems = document.getElementsByTagName("em");
for (var i=0;i<ems.length;i++) {
ems[i].style.fontStyle = "italic";
ems[i].style.fontWeight = "bold";
}
It is probably simpler to just create some CSS classes:
<style type="text/css">
em.bold {
font-weight:bold;
font-style:normal;
}
em.italic {
font-weight:normal;
font-style:italic;
}
</style>
You can then change all ems to one or the other:
var ems = document.getElementsByTagName("em");
for (var i=0;i<ems.length;i++) {
ems[i].className = "italic"; // or "bold"
}
If you care about IE 4, you can use
var ems = document.all.tags("em")
when the preferred document.getElementsByTagName isn't available.
var ems;
if (document.getElementsByTagName) {
ems = document.getElementsByTagName("em");
} else if (document.all && document.all.tags) {
ems = document.all.tags("em");
} else { // panic!
}
for (var i=0;i<ems.length;i++) {
ems[i].className = "italic"; // or "bold"
}
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'