Effect of making light rod with JavaScript and jQuery
- 2021-07-24 09:19:40
- OfStack
Differences and steps of adding CSS style using javaScript and jQuery
Using javaScript to make light rod effect
--The first is javaScript
<script>
$(function () {
var lis = document.getElementsByTagName("li"); // Definition DOM Variable acceptance label is li Elements of
for (var i = 0; i < lis.length;i++){
lis[i].onmouseover = function () {
// Mode 1
//this.style.backGround = "pink"; //1 Note that you can only use it here this Method acts as a for Loop the current traversal item
//this.style.fontSize = "50px"; //2 , likewise style The following additional styles can only be named by camel nomenclature
// Mode 2
this.style.cssText = "background-color:red;font-size:50px";
};
lis[i].onmouseout = function () {
// Mode 1
//this.style.background = "";
//this.style.fontSize = "20px";
// Mode 2
this.style.cssText = "background-color:;font-size:20px";
}
}
});
</script>
Compared with the two methods,. cssText is relatively simple
Using jQuery to make light rod effect
<script>
$(function () {
$("li").hover(function () { // Compound events are called here Simulate mouse hover events
$(this).css({"background-color": "red","font-size":"50px"});
},
function () {
$(this).css({ "background-color": "", "font-size": "20px" }); // Direct append CSS Style
}
);
});
</script>
Compared with javaScript, jQuer code is more flexible and simple. (There is automatic traversal effect in jQuery, which saves circulation.)