In order to get which button is being clicked, we have to make use of oEvent passed in:
<html>
<body onload = "load();">
<div>
<button id="button1" width = "100px">button1</button>
<button id="button2" width = "100px">button2</button>
</div>
</body>
<script>
function load() {
registerEvent("button1");
registerEvent("button2");
}
function registerEvent(buttonID) {
var oButton = document.getElementById(buttonID);
if( !oButton)
return;
oButton.onclick = function(oEvent) {
debugger;
alert("button clicked, id: " + oEvent.srcElement.id);
};
}
</script>
</html>
function registerEvent(buttonID) {
var oButton = document.getElementById(buttonID);
if( !oButton)
return;
oButton.onclick = (function() {
return function(){
alert("button clicked, id: " + oButton.id);
}
})();
}
The parameter oEvent is no longer needed.
Suppose when we try to log on WebQQ, the desktop background color should become dark to highlight the logon dialog.
Create a new div element used as a mask layer.
var createMask = function(){
return document,body.appendChild( document.createElement("div") );
};
$('button').click(function(){
var mask = createMask();
mask.show();
});
Drawback: It makes sense for the mask div element to be a singleton, or else every time we click log on button, there will be a new div generated.
I use the following code to achieve singleton.
var mask = document.body.appendChild(document.createElement(''div" ) );
$( ''button').click(function(){
mask.show();
});
Drawback: If log on button is never clicked, the mask div element is then not necessary. In this solution, the mask div is always created although not necessary.
I use a global variable to check whether the mask div has already been created or not.
var mask;
var createMask = function(){
if(mask)
return mask;
else {
mask = document,body.appendChild( document.createElement(div) );
return mask;
}
}
var createMask = function() {
var mask;
return function() {
return mask || ( mask = document.body.appendChild(document.createElement('div')));
}
}();
var singleton = function(fn){
var result;
return function() {
return result || ( result = fn.apply(this,arguments));
}
}
var createMask = singleton(function(){
return document.body.appendChild(document.createElement('div'));
});
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
6 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
2 | |
2 |