注册 登录
编程论坛 JavaScript论坛

请教 js 设置本地图片为网页背景

cblnuo 发布于 2014-12-07 15:02, 571 次点击
请教,如何让再浏览器打开网页时,加载本地图片为网页背景。使用
var loca_picpath="s:\ph\pic.jpg";
document.write("<style type='text/css'>body{ background:url("+local_picpath+") no-repeat; background-size:100% 100%; width:100%;height:100%;}</style>";
这样写加载不出来呢?
急,求指教。
2 回复
#2
渴望做梦2015-02-11 14:43
document.write("<style type='text/css'>body{ background:url("+local_picpath+") no-repeat; background-size:100% 100%; width:100%;height:100%;}</style>"
你上面的写法是在页面上打印输出这条代码,注意是打印再页面上,就是以文字的方式显示出来,而你需要的不是把代码打印再页面上而是执行它,所以要像下面这么写
document.body.style.background='url("图片地址")'
#3
冰镇柠檬汁儿2015-03-11 23:22
document.body.style.backgroundImage = 'url(imagepath.jpg)';
document.body.style.backgroundColor = '#da000a';
document.body.style.backgroundRepeat = 'no-repeat';
1