Javascript 有人能给代码加注释吗?
这是我的密码 此代码中的资产是什么? 和:解释资产的最佳位置 描述可用于处理交通灯序列的阵列的结构 编写一个脚本,使用所述数组生成一组交通灯的动画,以便每次单击按钮时,交通灯都会按标准顺序变化Javascript 有人能给代码加注释吗?,javascript,html,function,annotations,Javascript,Html,Function,Annotations,这是我的密码 此代码中的资产是什么? 和:解释资产的最佳位置 描述可用于处理交通灯序列的阵列的结构 编写一个脚本,使用所述数组生成一组交通灯的动画,以便每次单击按钮时,交通灯都会按标准顺序变化 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> &l
<!DOCTYPE html> <html>
<head>
<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>
<title>Traffic Light</title>
</head>
<div style="width:100px;height:360px;border:3px solid #000;">
<button>Change Lights</button>
<svg height="100" width="100">
<circle id=tf1 cx="50" cy="50" r="40" stroke="black" stroke-width="2"fill="red"/></svg>
<svg height="100" width="100">
<circle id="tf2" cx="50" cy="50" r="40" stroke="black" stroke-width="2"fill="transparent"/></svg>
<svg height="100" width="100">
<circle id="tf3" cx="50" cy="50" r="40" stroke="black" stroke-width="2"fill="transparent"/></svg>
</div>
</body>
</html>
i=1;
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
如果(i==1){
document.getElementById(“tf1”).setAttribute(“填充”、“透明”);
document.getElementById(“tf2”).setAttribute(“填充”、“黄色”);
i=2;
}else如果(i==2){
document.getElementById(“tf2”).setAttribute(“填充”、“透明”);
document.getElementById(“tf3”).setAttribute(“填充”、“绿色”);
i=3;
}else如果(i==3){
document.getElementById(“tf3”).setAttribute(“填充”、“透明”);
document.getElementById(“tf1”).setAttribute(“填充”、“红色”);
i=1;
}
});
});
交通灯
换灯
我还需要为每一行写一个详细的注释,我已经完成了最后一部分,我只需要这一部分的帮助。有人能帮我在deatil中注释这段代码吗
<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>
i=1;
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
如果(i==1){
document.getElementById(“tf1”).setAttribute(“填充”、“透明”);
document.getElementById(“tf2”).setAttribute(“填充”、“黄色”);
i=2;
}else如果(i==2){
document.getElementById(“tf2”).setAttribute(“填充”、“透明”);
document.getElementById(“tf3”).setAttribute(“填充”、“绿色”);
i=3;
}else如果(i==3){
document.getElementById(“tf3”).setAttribute(“填充”、“透明”);
document.getElementById(“tf1”).setAttribute(“填充”、“红色”);
i=1;
}
});
});
帮助将非常有用您没有使用正确的方法来创建属性。使用attr()更改属性样式 试试这个:
<div style="width:100px;height:360px;border:3px solid #000;">
<button>Change Lights</button>
<svg height="100" width="100">
<circle id=tf1 cx="50" cy="50" r="40" stroke="black" stroke-width="2"fill="red"/></svg>
<svg height="100" width="100">
<circle id="tf2" cx="50" cy="50" r="40" stroke="black" stroke-width="2"fill="transparent"/></svg>
<svg height="100" width="100">
<circle id="tf3" cx="50" cy="50" r="40" stroke="black" stroke-width="2"fill="transparent"/></svg>
</div>
下面是JSFIDLE代码链接:这不是StackOverflow的用途。您到底为什么需要帮助?有什么你不明白的吗?(然后要求这样做(尽管,如果你不理解,你是如何编写代码的))。还是你写的太多了?OP要求编写注释,而不是使用jQuery的代码版本。另外,“您没有使用适当的方法来表示属性”代码使用的是DOM方法,因此他们正确地使用了它:非常感谢,但我知道我没有正确地使用它,我必须保持这种状态,并详细解释代码中每一行的作用。
i=1;
jQuery(document).ready(function(){
jQuery("button").click(function(){
if(i==1){
jQuery("#tf1").attr("fill", "transparent");
jQuery("#tf2") .attr("fill", "yellow");
i=2;
}else if (i==2){
jQuery("#tf2").attr("fill", "transparent");
jQuery("#tf3") .attr("fill", "green");
i=3;
} else if(i==3) {
jQuery("#tf3").attr("fill", "transparent");
jQuery("#tf1").attr("fill", "red") ;
i=1;
}
});
});