티스토리 뷰
<button id="b1">Button 1</button>
<button id="b2">Button 2</button>
<script>
var b1 = document.getElementById('b1'), b2 = document.getElementById('b2');
var x = "Hello!";
function showX() {
alert(x);
}
b1.onclick = function() {
x = "button one";
showX();
};
b2.onclick = function() {
x = "button two";
showX();
};
</script>
데모
-------------------HTML :
<div id="mp3buttons">
<div title="mp3Player/kingRight.mp3">King Right</div>
<div title="mp3Player/AnotherSong.mp3">Another Song</div>
etc.
</div>
자바 스크립트 :
var mp3buttons = document.getElementById( 'mp3buttons' );
mp3buttons.onclick = function ( e ) {
var url = e.target.title;
// do stuff with this URL
};
따라서 래퍼 안에 버튼을 넣습니다. 각 버튼에 대해 title
속성 (또는 다른 적절한 속성) 내에 URL을 배치합니다 .
JavaScript 코드에서 클릭 핸들러를 래퍼에 바인딩합니다. 클릭 한 버튼은로 참조됩니다 e.target
.
출처
https://stackoverflow.com/questions/39917190