js阻止冒泡

(ev || event).cancelBubble = true;

标签切换

<script type="text/javascript">

window.onload = function () {

var oUl = document.getElementById('ul_menu');

var oLi = getByClass(oUl, 'item');

var sonUl = getByClass(oUl, 'son');

for (var i = 0; i < oLi.length; i++) {

oLi[i].index = i;

oLi[i].onclick = function (ev) {

for (var n = 0; n < oLi.length; n++) oLi[n].className = "";

this.className = "current";

for (var n = 0; n < sonUl.length; n++) sonUl[n].style.display = "none";

sonUl[this.index].style.display = "block";

(ev || event).cancelBubble = true;

}

}

document.onclick = function () {

for (var n = 0; n < oLi.length; n++) oLi[n].className = "";

for (var n = 0; n < sonUl.length; n++) sonUl[n].style.display = "none";

}

}

function getByClass(oParent, sClass) {

var aEle = oParent.getElementsByTagName('*');

var aResult = [];

var re = new RegExp('\\b' + sClass + '\\b', 'i');

var i = 0;

for (i = 0; i < aEle.length; i++) {

//if(aEle[i].className==sClass)

//if(aEle[i].className.search(sClass)!=-1)

if (re.test(aEle[i].className)) {

aResult.push(aEle[i]);

}

}

return aResult;

}

</script>

05-11 09:22