基础代码
测试div {
font-size: 2em;
width: 250px;
cursor: pointer /*变成手型*/
}
.clickme {
background-color: #2aabd2;
}
方法1-javascript代码绑定
document.getElementById("clickme").onclick = function () {
location.href = "http://www.baidu.com/"
};
方法2-jQuery代码绑定
$(function () {
$("#clickme").bind("click", function () {
location.href = "http://www.baidu.com/"
});
});
方法3-addEventListener()代码绑定
function eventBindClick(obj, href) {
eventBind(obj, 'click', go(href));
}
function eventBind(obj, eventType, callBack) {
if (obj.addEventListener) {
obj.addEventListener(eventType, callBack, false);
} else if (obj.attachEvent) {
//window.attachEvent
obj.attachEvent('on' + eventType, callBack);
}
}
function go(href) {
location.href = href;
}