关于单引号和双引号的问题,求大神指教。

dearestayuS 2017-02-24 05:24:24
chartBody += "</td><td><img alt='horiz bar' src=' "+imgSrc+" ' width="+thisChart.fields[j][i]*3+">";
chartBody += thisChart.fields[j][i]+"</td></tr>";

其中我的 imgSrc = "images/" + getButton("color");
函数getButton("color")返回的的是lilRed.gif
也就是说,我的imgSrc = "images/" + lilRed.gif

我想问在charBody里面为什么 src=' "+imgSrc+" ',而且单引号和双引号的位置不能交换?我记得在JS里面单引号和双引号应该是等价的吧?这段代码里面单双引号太多了,怪我理解无能。求大神指教
...全文
285 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
天际的海浪 2017-02-24
  • 打赏
  • 举报
回复
因为需要在输出的html标签中写入单引号啊。 当然,你的imgSrc变量的字符串中没有空格,可以不加引号。这是html标签的容错行为。 如果你的imgSrc变量的字符串中有空格,就必须加引号。 对比 <img src='my pictures/abc.jpg' /> 和 <img src=my pictures/abc.jpg />
dearestayuS 2017-02-24
  • 打赏
  • 举报
回复
引用 6 楼 u012418098 的回复:
[quote=引用 5 楼 dearestayuS 的回复:] [quote=引用 1 楼 u012418098 的回复:] "</td><td><img alt='horiz bar' src=' "+imgSrc+" ' width="+thisChart.fields[j][i]*3+">"切换以后效果都不一样 变成"</td><td><img alt='horiz bar' src=" '+imgSrc+' 很明显这里错了,没有拼接符 "width="+thisChart.fields[j][i]*3+">"
原句是这样的 chartBody += "<tr class='horiz'><td>"+thisChart.fieldnames[j]; chartBody += "</td><td><img alt='horiz bar' src='"+imgSrc; chartBody += "' width="+thisChart.fields[j][i]*3+">"; chartBody += thisChart.fields[j][i]+"</td></tr>"; 我似乎多打了一个+号,但是原句里面的imgSrc部分的单双引号我还是没看懂,求大神指点![/quote] 你要的效果是<div class="aa"></div> 或者<div class=‘aa’></div>本身已存在单引号或者双引号所以才要那样写.[/quote]可是这个多出来的引号是在src里呀,我的imgSrc="image/"+lilRed.gif,按道理应该不需要再加引号了呀,直接src=imgSrc就可以了吧?
dearestayuS 2017-02-24
  • 打赏
  • 举报
回复
引用 7 楼 jslang 的回复:
[quote=引用 4 楼 dearestayuS 的回复:] [quote=引用 2 楼 jslang 的回复:] 把这一行拆开来看你就明白了

chartBody += "</td><td><img alt='horiz bar' src=' " //第1个字符串
+
    imgSrc	 //第2个字符串
+
    " ' width="	 //第3个字符串
+
    thisChart.fields[j][i]*3	//第4个
+
    ">";	 //第5个字符串
我起初也是这么看的,但是在第一个和第三个字符串里就会多出一个单引号了呀,单引号就不是成对出现了[/quote]
引用 4 楼 dearestayuS 的回复:
[quote=引用 2 楼 jslang 的回复:] 把这一行拆开来看你就明白了

chartBody += "</td><td><img alt='horiz bar' src=' " //第1个字符串
+
    imgSrc	 //第2个字符串
+
    " ' width="	 //第3个字符串
+
    thisChart.fields[j][i]*3	//第4个
+
    ">";	 //第5个字符串
我起初也是这么看的,但是在第一个和第三个字符串里就会多出一个单引号了呀,单引号就不是成对出现了[/quote] 当三个字符串拼接在一起就成对了[/quote]那个单引号起什么作用呢?
天际的海浪 2017-02-24
  • 打赏
  • 举报
回复
引用 4 楼 dearestayuS 的回复:
[quote=引用 2 楼 jslang 的回复:] 把这一行拆开来看你就明白了

chartBody += "</td><td><img alt='horiz bar' src=' " //第1个字符串
+
    imgSrc	 //第2个字符串
+
    " ' width="	 //第3个字符串
+
    thisChart.fields[j][i]*3	//第4个
+
    ">";	 //第5个字符串
我起初也是这么看的,但是在第一个和第三个字符串里就会多出一个单引号了呀,单引号就不是成对出现了[/quote]
引用 4 楼 dearestayuS 的回复:
[quote=引用 2 楼 jslang 的回复:] 把这一行拆开来看你就明白了

chartBody += "</td><td><img alt='horiz bar' src=' " //第1个字符串
+
    imgSrc	 //第2个字符串
+
    " ' width="	 //第3个字符串
+
    thisChart.fields[j][i]*3	//第4个
+
    ">";	 //第5个字符串
我起初也是这么看的,但是在第一个和第三个字符串里就会多出一个单引号了呀,单引号就不是成对出现了[/quote] 当三个字符串拼接在一起就成对了
jio可 2017-02-24
  • 打赏
  • 举报
回复
引用 5 楼 dearestayuS 的回复:
[quote=引用 1 楼 u012418098 的回复:] "</td><td><img alt='horiz bar' src=' "+imgSrc+" ' width="+thisChart.fields[j][i]*3+">"切换以后效果都不一样 变成"</td><td><img alt='horiz bar' src=" '+imgSrc+' 很明显这里错了,没有拼接符 "width="+thisChart.fields[j][i]*3+">"
原句是这样的 chartBody += "<tr class='horiz'><td>"+thisChart.fieldnames[j]; chartBody += "</td><td><img alt='horiz bar' src='"+imgSrc; chartBody += "' width="+thisChart.fields[j][i]*3+">"; chartBody += thisChart.fields[j][i]+"</td></tr>"; 我似乎多打了一个+号,但是原句里面的imgSrc部分的单双引号我还是没看懂,求大神指点![/quote] 你要的效果是<div class="aa"></div> 或者<div class=‘aa’></div>本身已存在单引号或者双引号所以才要那样写.
dearestayuS 2017-02-24
  • 打赏
  • 举报
回复
引用 1 楼 u012418098 的回复:
"</td><td><img alt='horiz bar' src=' "+imgSrc+" ' width="+thisChart.fields[j][i]*3+">"切换以后效果都不一样 变成"</td><td><img alt='horiz bar' src=" '+imgSrc+' 很明显这里错了,没有拼接符 "width="+thisChart.fields[j][i]*3+">"
原句是这样的 chartBody += "<tr class='horiz'><td>"+thisChart.fieldnames[j]; chartBody += "</td><td><img alt='horiz bar' src='"+imgSrc; chartBody += "' width="+thisChart.fields[j][i]*3+">"; chartBody += thisChart.fields[j][i]+"</td></tr>"; 我似乎多打了一个+号,但是原句里面的imgSrc部分的单双引号我还是没看懂,求大神指点!
dearestayuS 2017-02-24
  • 打赏
  • 举报
回复
引用 2 楼 jslang 的回复:
把这一行拆开来看你就明白了

chartBody += "</td><td><img alt='horiz bar' src=' " //第1个字符串
+
    imgSrc	 //第2个字符串
+
    " ' width="	 //第3个字符串
+
    thisChart.fields[j][i]*3	//第4个
+
    ">";	 //第5个字符串
我起初也是这么看的,但是在第一个和第三个字符串里就会多出一个单引号了呀,单引号就不是成对出现了
Hello World, 2017-02-24
  • 打赏
  • 举报
回复
用单引用或者双引号都是要成对出现的,你拼接字符串时开头用的是双引号,结尾也需要双引号,中间如果要用双引号就需要转义:\"
天际的海浪 2017-02-24
  • 打赏
  • 举报
回复
把这一行拆开来看你就明白了

chartBody += "</td><td><img alt='horiz bar' src=' " //第1个字符串
+
    imgSrc	 //第2个字符串
+
    " ' width="	 //第3个字符串
+
    thisChart.fields[j][i]*3	//第4个
+
    ">";	 //第5个字符串
jio可 2017-02-24
  • 打赏
  • 举报
回复
"</td><td><img alt='horiz bar' src=' "+imgSrc+" ' width="+thisChart.fields[j][i]*3+">"切换以后效果都不一样 变成"</td><td><img alt='horiz bar' src=" '+imgSrc+' 很明显这里错了,没有拼接符 "width="+thisChart.fields[j][i]*3+">"

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧