我有一个带图片的页面。当有人点击图片时,我想打开此页面并显示点击的图片。但似乎脚本只在刷新后运行!
<body>
<img src="whatImg()" id="image"/>
//这是脚本
function whatImg(){
var str = getUrlVars()["obj"];
document.getElementById('image').src = str;}
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m, key, value) {vars[key] = value;});
return vars;
}
whatImg();
</script>
</body>
答案 0 :(得分:0)
您要做的是更改脚本中的src
属性。
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<img src="blank.gif" id="image"/>
<script>
function whatImg(){
var str = getUrlVars()["obj"];
document.getElementById('image').src = str;
}
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m, key, value) {vars[key] = value;});
return vars;
}
whatImg();
</script>
</body>
</html>