Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.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
向url分配变量失败(JavaScript DOM)_Javascript_Html_Dom_Background Image - Fatal编程技术网

向url分配变量失败(JavaScript DOM)

向url分配变量失败(JavaScript DOM),javascript,html,dom,background-image,Javascript,Html,Dom,Background Image,为什么URL不接受这样的变量值 设pic='img_tree.png'; document.body.style.backgroundImage=“url(pic)” 但是接受像这样的“pic”变量值。 document.body.style.backgroundImage=“url(“+pic+”)” 你好,世界! 设置背景图像 函数myFunction(){ 设pic='img_tree.png'; //document.body.style.backgroundImage=“url(p

为什么URL不接受这样的变量值 设pic='img_tree.png'; document.body.style.backgroundImage=“url(pic)”

但是接受像这样的“pic”变量值。 document.body.style.backgroundImage=“url(“+pic+”)”


你好,世界!
设置背景图像
函数myFunction(){
设pic='img_tree.png';
//document.body.style.backgroundImage=“url(pic)”;错误
document.body.style.backgroundImage=“url(“+pic+”)”;//工作正常
}
因为当它在引号中时,它只是字母“p”、“i”和“c”。但当它在引号之外时,它是一个用于查找值的标识符

document.body.style.backgroundImage = "url(pic)";
//                                    ^^^^^^^^^^−−−−−− string, the letters inside
//                                                     have no special meaning to
//                                                     JavaScript
vs

因为当它在引号中时,它只是字母“p”,“i”和“c”。但当它在引号之外时,它是一个用于查找值的标识符

document.body.style.backgroundImage = "url(pic)";
//                                    ^^^^^^^^^^−−−−−− string, the letters inside
//                                                     have no special meaning to
//                                                     JavaScript
vs