2017년 6월 3일 토요일

JavaScript : id 대신 class를 사용해서 요소 제어하기

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head> 
<title> New Document </title> 
<meta name="Generator" content="EditPlus"> 
<meta name="Author" content=""> 
<meta name="Keywords" content=""> 
<meta name="Description" content=""> 
<script language="JavaScript"> 
<!-- 
function getElementsByClassName1(className) {  
    var children = document.getElementsByTagName('*') || document.all;  
    var elements = [];  
    for(var i=0, l=children.length; i<l; i++) {  
        var childClass = (children[i].className) ? children[i].className.split(' ') : false;  
        if(childClass)  
            for(var n=0, m=childClass.length; n<m; n++)  
                if(childClass[n] == className) elements.push(children[i]);  
    }  
    return elements;  

}  

function fnBold(str) { 
    elements = getElementsByClassName1("menu_a"); 
    for (i = 0; i < elements.length; i++) { 
        elements[i].style.fontWeight = str; 
    } 
//--> 
</script> 
</head> 

<body> 
<div class="menu_a">첫번째 menu_a</div>  
<div class="menu_a">두번째 menu_a</div>  
<div class="menu_a">세번째 menu_a</div>  

<A HREF="javascript:fnBold('bold')">굵게</A> 
<A HREF="javascript:fnBold('')">가늘게</A> 
</body> 
</html>