在没有jquery库的情况下使用jQuery悬停吗? [英] Use jQuery hover without jquery library?
本文介绍了在没有jquery库的情况下使用jQuery悬停吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用以下代码:
Is there a way to use the following code:
$(document).ready(function () {
$('sd').hover(function() {
$(this).addClass('a');
}, function() {
$(this).removeClass('a');
});
});
在我的代码中不包含jquery文件吗?例如,有没有办法使用纯JavaScript做完全相同的事情?
without including the jquery file in my code? For example, is there a way to do exactly the same thing with pure javascript?
推荐答案
Sure, modified from this answer: Change an element's class with JavaScript
window.onload = function() {
var elements = document.getElementsByTagName('sd');
for (var i in elements) {
if (!elements.hasOwnProperty(i)) continue;
elements[i].addEventListener( 'mouseover', function() {
this.className += 'a';
}
elements[i].addEventListener( 'mouseout', function() {
this.className = this.className.replace( /(?:^|\s)a(?!\S)/g , '' );
}
}
}
这篇关于在没有jquery库的情况下使用jQuery悬停吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文