简体   繁体   English

img src变量在jQuery中不起作用

[英]img src variable not working in jQuery

The application is working on dreamweaver but when I try it on Chrome or IE it dones't work. 该应用程序可以在Dreamweaver上运行,但是当我在Chrome或IE上尝试时,它无法正常工作。 I know where the issue is but I don't now how to fix it. 我知道问题出在哪里,但现在不知道如何解决。 It has something to do with applying variable as an img src. 它与将变量用作img src有关。 The code is setup at http://jsfiddle.net/v9rxH/ 该代码是在http://jsfiddle.net/v9rxH/上设置的

the issue is in the line 问题在排队

 $(".topImage").attr("src", "'" + obj[randomNumA].urlTop + "'");
 $(".middleImage").attr("src", "'" + obj[randomNumB].urlMiddle + "'");
 $(".bottomImage").attr("src", "'" + obj[randomNumC].urlBottom + "'");

when the browser renders it is show 当浏览器渲染时显示

 <img src="'http://placehold.it/300x100&text=SecondBottom'" class="bottomImage">

it's adding additional single quotes(' ') in src. 它在src中添加了其他单引号('')。 Based on that, I removed the single quotes and it shows the variable as text instead of it's value. 基于此,我删除了单引号,并将变量显示为文本而不是其值。 What am I missing? 我想念什么? Thanks in advance. 提前致谢。 Rex 雷克斯

You're putting the single quotes around it with "'" - you don't need to, using .attr() handles that for you: 您可以在单引号周围加上"'" ,而不必使用.attr()为您处理:

$(".topImage").attr("src", obj[randomNumA].urlTop);
$(".middleImage").attr("src", obj[randomNumB].urlMiddle);
$(".bottomImage").attr("src", obj[randomNumC].urlBottom);

Remove single quotes from obj[randomNumN].urlTop . obj[randomNumN].urlTop删除单引号。

Try, 尝试,

 $(".topImage").attr("src", obj[randomNumA].urlTop);
 $(".middleImage").attr("src", obj[randomNumB].urlMiddle);
 $(".bottomImage").attr("src", obj[randomNumC].urlBottom);

Use this instead: 使用此代替:

$(".topImage").attr("src", obj[randomNumA].urlTop);
$(".middleImage").attr("src", obj[randomNumB].urlMiddle);
$(".bottomImage").attr("src", obj[randomNumC].urlBottom);

jsFiddle example. jsFiddle示例。

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

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