본문 바로가기

개발하자/자바스크립트

객체생성이용,외부js파일이용,클릭이벤트 등록하기

a.js
====
function Button(a,b){

var inputTag=document.createElement("input");
inputTag.type="button";
inputTag.id=a;
inputTag.className=b;

buttonContainer.appendChild(inputTag);

this.addActionListener=function(callbackFunc){
inputTag.onclick=callbackFunc;
}
}

b.html
======
<html>
<head>
<style type="text/css">
.addStyle { background:orange; }
.removeStyle { background:skyblue; }
</style>
<script src="a.js"></script>
<script>
window.onload=function(){
initButton();
}
function initButton(){
var b1=new Button("addButton","addStyle");
var b2=new Button("removeButton","removeStyle");
b1.addActionListener(f1);
}
function f1(){
alert(111);
}
</script>
</head>
<body>
<div id="buttonContainer">
</div>
</body>
</html>