简体   繁体   English

如何在字符串中添加双正斜杠而不在 Javascript 中注释掉?

[英]How to add double forward slash in a string without commenting out in Javascript?

I would like to create a string but when I create the string variable the double forward slash comments out the rest of the line.我想创建一个字符串,但是当我创建字符串变量时,双正斜杠会注释掉该行的其余部分。 How do I create a string without commenting out part of the string?如何在不注释掉部分字符串的情况下创建字符串?

example:例子:

var linkVariable = "http://free.clipartof.com/176-Free-Cartoon-Owl-Clipart.jpg"

Everything after the // gets commented out. //之后的所有内容都被注释掉了。

you create a string in javascript by putting the string inside quotations:您可以通过将字符串放在引号中来在 javascript 中创建一个字符串:

var answer = "It's alright";
var answer = "He is called 'Johnny'";
var answer = 'He is called "Johnny"';

Which you have correctly done.你做对了。

What you are seeing is likely syntax highlighting from your code editor/IDE.您所看到的可能是您的代码编辑器/IDE 中的语法突出显示。

A good tool, as you are getting started, is jsfiddle.一个很好的工具,当你开始时,是 jsfiddle。

I have set up a jsFiddle for you with the following code:我已经使用以下代码为您设置了一个 jsFiddle:

 var linkVariable = "http://free.clipartof.com/176-Free-Cartoon-Owl-Clipart.jpg" document.getElementById('link').value = linkVariable;
 <input style="width:300px;" type="text" id="link"></input>

also available at也可在

http://jsfiddle.net/chrislewispac/yte9f5yv/ http://jsfiddle.net/chrislewispac/yte9f5yv/

Where you can see the value of your string variable in an input box.您可以在输入框中查看字符串变量的值。

Go take a look and play around creating the different types in javascript.去看看并尝试在 javascript 中创建不同的类型。 I've learned a lot from isolating problems and testing it in an external environment!我从隔离问题并在外部环境中测试它学到了很多!

Also, your code editor probably has settings that can be adjusted to fix this for you.此外,您的代码编辑器可能具有可以调整的设置来为您解决此问题。

Edit: (since you are using Sublime Text) In sublime text you can go to Preferences -> Browse Packages -> Javascript and find the file Comments.tmPreferences .编辑:(因为您使用的是 Sublime Text)在 sublime text 中,您可以转到 Preferences -> Browse Packages -> Javascript 并找到文件 Comments.tmPreferences 。 You can then edit your comments preferences.然后您可以编辑您的评论首选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM