学习 DOM0 javascript,教授抛出了一个曲线球。他用班级名称写了几十个段落:体育、生活、书籍、喜剧,然后是:
<p class="sports books comedy">
I found this book: http://www.amazon.com/Teed-Off-Laughs-Greens-Sports/dp/0233995005/ref=sr_1_1?ie=UTF8&s=books&qid=1277749796&sr=1-1 You don't have to be good to play, but a low level of self-esteem is helpful! Humorous collection of colour photographs and hilarious text reduces our heroes of the golf course to mere mortals. No golfing disaster is left secret and the 80 photographs only go to prove the fallibility of international stars.
</p>
必须在不使用 Jquery 的情况下对其进行样式设置,我的想法是:
window.onload=function(){
var elements = document.getElementsByTagName('*');
for (var i=0; i<elements.length; i++) {
if (elements[i].className == 'sports') {
elements[i].style.border="2px solid green";
}
else if (elements[i].className == 'life') {
elements[i].style.color="red";
}
else if (elements[i].className=="books") {
elements[i].style.textAlign="right";
}
else if (elements[i].className=="comedy") {
elements[i].style.fontSize="200%";
}
}
}
它适用于除多个类名之外的所有内容。试图找到解决它的方法。