gpt4 book ai didi

javascript - 有人可以解释以下 javascript 代码吗?

转载 作者:行者123 更新时间:2023-11-30 06:56:50 24 4
gpt4 key购买 nike

除了解释,javascript中的$是什么意思?这是代码:

var ZebraTable = {
bgcolor: '',
classname: '',
stripe: function(el) {
if (!$(el)) return;
var rows = $(el).getElementsByTagName('tr');
for (var i=1,len=rows.length;i<len;i++) {
if (i % 2 == 0) rows[i].className = 'alt';
Event.add(rows[i],'mouseover',function() {
ZebraTable.mouseover(this); });
Event.add(rows[i],'mouseout',function() { ZebraTable.mouseout(this); });
}
},
mouseover: function(row) {
this.bgcolor = row.style.backgroundColor;
this.classname = row.className;
addClassName(row,'over');
},
mouseout: function(row) {
removeClassName(row,'over');
addClassName(row,this.classname);
row.style.backgroundColor = this.bgcolor;
}
}

window.onload = function() {
ZebraTable.stripe('mytable');
}

这是我获得代码的链接,您可以在页面上查看演示。它似乎没有使用任何框架。我实际上正在阅读一个 JQuery 教程,该教程使用这段代码并在其上使用 JQuery 来执行表 strip 化。这是链接:

http://v3.thewatchmakerproject.com/journal/309/stripe-your-tables-the-oo-way

最佳答案

Can someone explain the following javascript code?

//Shorthand for document.getElementById
function $(id) {
return document.getElementById(id);
}

var ZebraTable = {
bgcolor: '',
classname: '',

stripe: function(el) {
//if the el cannot be found, return
if (!$(el)) return;

//get all the <tr> elements of the table
var rows = $(el).getElementsByTagName('tr');

//for each <tr> element
for (var i=1,len=rows.length;i<len;i++) {

//for every second row, set the className of the <tr> element to 'alt'
if (i % 2 == 0) rows[i].className = 'alt';

//add a mouseOver event to change the row className when rolling over the <tr> element
Event.add(rows[i],'mouseover',function() {
ZebraTable.mouseover(this);
});

//add a mouseOut event to revert the row className when rolling out of the <tr> element
Event.add(rows[i],'mouseout',function() {
ZebraTable.mouseout(this);
});
}
},

//the <tr> mouse over function
mouseover: function(row) {
//save the row's old background color in the ZebraTable.bgcolor variable
this.bgcolor = row.style.backgroundColor;

//save the row's className in the ZebraTable.classname variable
this.classname = row.className;

//add the 'over' class to the className property
//addClassName is some other function that handles this
addClassName(row,'over');
},
mouseout: function(row) {
//remove the 'over' class form the className of the row
removeClassName(row,'over');

//add the previous className that was stored in the ZebraTable.classname variable
addClassName(row,this.classname);

//set the background color back to the value that was stored in the ZebraTable.bgcolor variable
row.style.backgroundColor = this.bgcolor;
}
}

window.onload = function() {
//once the page is loaded, "stripe" the "mytable" element
ZebraTable.stripe('mytable');
}

关于javascript - 有人可以解释以下 javascript 代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/731810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com