[英]what does this function do and what does it mean ?`
I'm trying to understand what each line of this code does eg why is the $
there and what does it do?, can anyone help?我试图了解这段代码的每一行是做什么的,例如为什么$
在那里,它有什么作用?,有人可以帮忙吗?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<head>
<script>
i=1;
$(document).ready(function(){
$("button").click(function(){
if(i==1){
document.getElementById("tf1").setAttribute("fill", "transparent");
document.getElementById("tf2") .setAttribute("fill", "yellow");
i=2;
}else if (i==2){
document.getElementById("tf2").setAttribute("fill", "transparent");
document.getElementById("tf3") .setAttribute("fill", "green");
i=3;
} else if(i==3) {
document.getElementById("tf3").setAttribute("fill", "transparent");
document.getElementById("tf1").setAttribute("fill", "red") ;
i=1;
}
});
});
</script>
$- is for defining/accessing jQuery. $- 用于定义/访问 jQuery。
$(document) - means your whole file is under consideration. $(document) - 表示正在考虑您的整个文件。 $("button") - means all buttons in your document. $("button") - 表示文档中的所有按钮。
Now the code, If the button is clicked and i is 1, then any element with id tf1 will have transparent color and any element with id tf2 will have yellow color.Then set i to 2.现在的代码,如果单击按钮并且 i 为 1,则任何具有 id tf1 的元素将具有透明颜色,具有 id tf2 的任何元素将具有黄色。然后将 i 设置为 2。
If the button is clicked and i is 2, then any element with id tf2 will have transparent color and any element with id tf3 will have green color.如果单击按钮并且 i 为 2,则任何 id 为 tf2 的元素将具有透明颜色,而任何 id 为 tf3 的元素将具有绿色。 Then set i to 3.然后将 i 设置为 3。
If the button is clicked and i is 3, then any element with id tf3 will have transparent color and any element with id tf1 will have red color.Then set i to 1.如果单击按钮并且 i 为 3,则任何具有 id tf3 的元素将具有透明颜色,任何具有 id tf1 的元素将具有红色。然后将 i 设置为 1。
So, whenever a button is clicked the value of i will be set different every time and your document elements with those ids will change their color every time.因此,每当单击按钮时, i 的值每次都会设置不同,并且具有这些 id 的文档元素每次都会更改其颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.