我想转换它,所以我不需要加载库
$('.mobile .pureCssMenu span , .tablet .pureCssMenu span').on('click',function(e){
e.preventDefault();
return false;
});
答案 0 :(得分:3)
document.querySelectorAll与css选择器一起使用:
var elements=document.querySelectorAll('.mobile .pureCssMenu span , .tablet .pureCssMenu span');
for(var i = 0;i < elements.length;i++){
elements[i].onclick=function(e){
e.preventDefault();
return false;
}
}
答案 1 :(得分:1)
使用forEach
,call
和addEventListener
方法的方法:
var elements = document.querySelectorAll('.mobile .pureCssMenu span , .tablet .pureCssMenu span')
Array.prototype.forEach.call(elements, function (elem) {
elem.addEventListener('click', function (evt) {
evt.preventDefault()
return false
})
})
答案 2 :(得分:1)
更好的方法:
var elements = document.querySelectorAll('.mobile .pureCssMenu span , .tablet .pureCssMenu span');
for (var i = 0, il = elements.length; i <= il; i += 1) {
addEvent(elements[i], 'click', function (event) {
event = event || window.event;
event.preventDefault ? event.preventDefault() : (event.returnValue = false);
return false;
});
}
function addEvent(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback);
} else {
element.attachEvent(eventName, callback);
}
}