向url分配变量失败(JavaScript DOM)
为什么URL不接受这样的变量值 设pic='img_tree.png'; document.body.style.backgroundImage=“url(pic)” 但是接受像这样的“pic”变量值。 document.body.style.backgroundImage=“url(“+pic+”)”向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
你好,世界!
设置背景图像
函数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