2011-02-16 4 views
1
이는 onclick 속성과 작동

:는 작동하지만 동등한 함수를 호출하지 않을 경우

<button type="button" onclick="this.style.color='red';">Astringents</button> 

이것은 온 클릭 속성에 작동하지만 헤드 요소에서 호출되지 않은 경우 :

<head> 
    <script type="text/javascript"> 
    function red() { 
     this.style.color='red'; 
    } 
    </script> 
</head> 

<button type="button" onclick="red();">Astringents</button> 

편집 : 몇 가지 버튼을 제공하는 하나의 기능을 사용하고 싶습니다. 예를 들어 : - 요소의 속성을 편집 할 수 없습니다, 따라서이 시나리오에서

<button type="button" onclick="red();">Astringents</button> 
<button type="button" onclick="red();">Exfoliators</button> 
<button type="button" onclick="red();">Moisturizers</button> 
<button type="button" onclick="red();">Masques</button/> 

답변

4

, this<button> 요소를 참조하지 않습니다. 이 주제에 기사의 하단에있는 예를 바탕으로 http://www.quirksmode.org/js/this.html

의 쿼크 모드 기사 밖으로

검사는 다음과 같은 작업을해야.

<head> 
<script type="text/javascript"> 

function red(obj) { 
    obj.style.color='red'; 
} 
</script> 
</head> 

<button type="button" onclick="red(this)">Astringents</button> 
+0

감사합니다. – Corey

관련 문제