简体   繁体   English

为什么删除回车后我的 javascript 不起作用?

[英]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.

相关问题 为什么 Javascript 回车是两个时只算一个字符? - Why does Javascript only count carriage returns as one character when it is two? 当我使用“和”或“或”时,为什么我的替换在javascript中正常工作,但在我使用“&”或“/”或“+”时却没有 - Why does my replace work properly in javascript when I use “and” or “or” but not when I use “&” or “/” or “+” 当我在函数中使用if语句时,为什么我的javascript无法正常工作 - Why does my javascript not work when I use an if statement in my function 为什么我的代码只能在html中工作,而在将其放入Javascript页面时却不能工作? - Why does my code only works in html but does not work when I put it into a Javascript page? 为什么我的正则表达式可以在RegexPal中工作,但在运行Javascript时却不能工作? - Why does my regular expression work in RegexPal, but not when I run my Javascript? Javascript:回车如何工作? - Javascript: How does the carriage return work? 为什么我的javascript不起作用 - Why does my javascript not work 为什么此代码可用于我的数组,但不能输入字符串数组呢? 的JavaScript - Why does this code work with my array, but not when I typed in an array of strings? JavaScript 当我在HTML中调用它时,为什么我的Javascript函数无法运行/工作? - Why does my Javascript function not run/work when I call it in HTML? 当我在for循环中复制变量时,为什么我的JavaScript代码有效 - Why does my JavaScript code work when I copy a variable in a for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM