<html>
<head>
<title>示例页面</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<style type="text/css">
html, body {
background:#3d71b8 url(image/spin.jpg) no-repeat top center;
font: normal 12px tahoma, arial, verdana, sans-serif;
margin: 0;
padding: 0;
border: 0 none;
overflow: hidden;
height: 100%;
}
</style>
<script type="text/javascript">
window.onload = function(){
var img_name = document.location.href.split("=")[1];//值为shop
var img_obj = document.body.currentStyle.backgroundImage;//值为:"url("http://10.16.133.138:8080/zjkdsc/business/process/image/ship.jpg")";
alert(img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi,function($0,$1){return $0.replace($1,img_name);})); //值为:"url("http://10.16.133.138:8080/zjkdsc/business/process/image/shop.jpg")";
document.body.currentStyle.backgroundImage = img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi,function($0,$1){return $0.replace($1,img_name);});
//报脚本错,无效的过程调用或参数.
};
</script>
</head> <body >
</body></html>
<head>
<title>示例页面</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<style type="text/css">
html, body {
background:#3d71b8 url(image/spin.jpg) no-repeat top center;
font: normal 12px tahoma, arial, verdana, sans-serif;
margin: 0;
padding: 0;
border: 0 none;
overflow: hidden;
height: 100%;
}
</style>
<script type="text/javascript">
window.onload = function(){
var img_name = document.location.href.split("=")[1];//值为shop
var img_obj = document.body.currentStyle.backgroundImage;//值为:"url("http://10.16.133.138:8080/zjkdsc/business/process/image/ship.jpg")";
alert(img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi,function($0,$1){return $0.replace($1,img_name);})); //值为:"url("http://10.16.133.138:8080/zjkdsc/business/process/image/shop.jpg")";
document.body.currentStyle.backgroundImage = img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi,function($0,$1){return $0.replace($1,img_name);});
//报脚本错,无效的过程调用或参数.
};
</script>
</head> <body >
</body></html>
document.body.currentStyle.backgroundImage
====>
document.body.style.backgroundImage
<html>
<head>
<title>示例页面</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<style type="text/css">
html, body {
background:#3d71b8 url(image/spin.jpg) no-repeat top center;
font: normal 12px tahoma, arial, verdana, sans-serif;
margin: 0;
padding: 0;
border: 0 none;
overflow: hidden;
height: 100%;
}
</style>
<script type="text/javascript">
window.onload = function(){
var img_name = document.location.href.split("=")[1];//值为shop
var img_obj = document.body.currentStyle.backgroundImage;//值为:"url("http://10.16.133.138:8080/zjkdsc/business/process/image/ship.jpg")";
alert(img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi,function($0,$1){return $0.replace($1,img_name);})); //值为:"url("http://10.16.133.138:8080/zjkdsc/business/process/image/shop.jpg")";
document.body.style.backgroundImage = img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi,function($1,$1){return $1.replace($1,img_name);});
//报脚本错,无效的过程调用或参数.
};
</script>
</head> <body >
</body></html>
document.body.style.backgroundImage = "url('img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi,function($0,$1){return $0.replace($1,img_name);})')";
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
};
window.onload = function(){
var img_name = document.location.href.split("=")[1];
var img_obj = getCurrentStyle(document.body).backgroundImage;
alert(img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi, function($0, $1){
return $0.replace($1, img_name);
}));
document.body.style.backgroundImage = img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi, function($0, $1){
return $0.replace($1, img_name);
});
};
document.body.style.backgroundImage = "url('" + img_obj.replace(/(?:image\/)(.*)(?:\.jpg)/gi,function($0,$1){return $0.replace($1,img_name);}) + "')";
return $0.replace($1, img_name);
}));回调函数引用外部变量img_name,格外加上了一个双引号.导致图片出不来,为什么.注意这里.
"url("http://10.16.133.138:8080/zjkdsc/business/process/image/"shop".jpg")"shop上的双引号,为什么会这样.直接 return $0.replace($1, "shop");就不会有这样的问题
惭愧,框架用多了,自己写原生代码都退步了.
忽略了一些细节..
//你写的代码怎么这么长?
//不可以这样么
var img_name = document.location.href.split("=")[1];//值为shop
var img_obj = document.body.currentStyle.backgroundImage;//值为:"url("http://10.16.133.138:8080/zjkdsc/business/process/image/ship.jpg")";
var img_name_old = img_obj.split("process/image/")[1].split(".jpg").[0];//值为:ship
document.body.currentStyle.backgroundImage = img_obj.split(img_name_old).join(img_name);