Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中检查字符串值_Javascript - Fatal编程技术网

如何在javascript中检查字符串值

如何在javascript中检查字符串值,javascript,Javascript,我试图比较字符串的值,如果前两个值=@@n则调用一个div 这是我的代码示例,但似乎不起作用。这是正确的方法还是我偏离了目标。温柔一点,我是javascript新手 <script type = "text/javascript"> var str = "@@this is the line for the test"; if (str.substring(0,2) == "@@" { document.write(str.substring(0,2)); } else {

我试图比较字符串的值,如果前两个值=@@n则调用一个div

这是我的代码示例,但似乎不起作用。这是正确的方法还是我偏离了目标。温柔一点,我是javascript新手

<script type = "text/javascript">
var str = "@@this is the line for the test";
if (str.substring(0,2) == "@@" {
    document.write(str.substring(0,2));
} else {
    document.write("found nothing");
}
</script>

var str=“@@n这是测试的行”;
如果(str.substring(0,2)=“@”{
编写(str.substring(0,2));
}否则{
文件。填写(“未发现任何内容”);
}

测试
确实返回true,因此问题并不存在。我强烈建议使用
==
而不是
=
。区别在于,对于三个字符串,您在JavaScript中有一个严格的比较(因此在大多数其他语言中它相当于
=

如果您正在使用
document.write
进行测试,您可能会发现问题。请尝试改用
console.log
测试输出,并检查控制台的结果

此外,对于像这样的小事情,只需打开开发人员工具并在其中键入代码即可快速查看结果,您不需要脚本标记,只需键入JavaScript:)


var str=“@@n这是测试的行”;
if(str.substring(0,2)==“@”){
编写(str.substring(0,2));
}否则{
文件。填写(“未发现任何内容”);
}
尝试上面的代码,您只是缺少条件的右括号

此外,请参阅此链接,了解在比较中是使用==还是==(摘要使用==):

您只是在if语句中缺少了一个结束语(
F12开发人员工具控制台是您的朋友……太棒了。。谢谢你的反馈。。现在开始工作了!我想测试字符串的值,并根据输入返回不同的样式。但不起作用:)函数returndiv(str){if(str.substring(0,2)==“@”){return''str;}否则{return''str;}}在html中我会这样称呼它11:54:1717-12-19returndiv('@@ALERT HALL')
-DX机器人
<script type = "text/javascript">
var str = "@@this is the line for the test";
if (str.substring(0,2) === "@@") {
    document.write(str.substring(0,2));
} else {
    document.write("found nothing");
}
</script>