[英]Why does my javascript not work when I remove carriage returns?
Working javascript example is as follows...工作 javascript 示例如下...
<script>var count=1; function setColor(button) { var property = document.getElementById(button); if (count == 0) {property.style.color="red"
count=1;}else{property.style.color="green"
count=0;}}</script>
Broken javascript example is as follows...破碎的javascript示例如下...
<script>var count=1; function setColor(button) { var property = document.getElementById(button); if (count == 0) {property.style.color="red" count=1;}else{property.style.color="green" count=0;}}</script>
The html... html...
<a href="#" id="button" style= "color:red" onclick="setColor('button'); playlist.toggleShuffle();">SHUFFLE</a>
Any help understanding this would be great.任何帮助理解这一点都会很棒。
property.style.color="red" count=1;
and property.style.color="green" count=0;
和property.style.color="green" count=0;
are both not valid JavaScript.均无效 JavaScript。 When you're not using carriage returns, you need a semicolon to delineate the end of the line after "red"
and "green"
.当您不使用回车时,您需要一个分号来在"red"
和"green"
之后划定行尾。
What you're looking for is more like:您正在寻找的更像是:
<script>var count=1; function setColor(button) { var property = document.getElementById(button); if (count == 0) {property.style.color="red"; count=1;}else{property.style.color="green"; count=0;}}</script>
This feature is referred to as semicolon insertion;此功能称为分号插入; you can read more about it at this link .您可以在此链接上阅读更多相关信息。
... however I'm really not sure why you'd want to do this, as this saves maybe a byte or two here and there and offers no performance benefit, while nuking most of the readability of your code. ...但是我真的不确定您为什么要这样做,因为这可能会在这里和那里节省一两个字节并且不会提供任何性能优势,同时会破坏代码的大部分可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.