这是我的代码:我想问
<script type="text/javascript">
var total = 4;
</script>
我该怎么做?
<img src="img/apple_" + total + ".png" id="imageBox"/>
我一直在尝试使用call函数和document.onload,但根本不起作用,有谁能帮帮我?
这是我的代码:我想问
<script type="text/javascript">
var total = 4;
</script>
我该怎么做?
<img src="img/apple_" + total + ".png" id="imageBox"/>
我一直在尝试使用call函数和document.onload,但根本不起作用,有谁能帮帮我?
我猜您只是想使用JavaScript更新图像的src。
document.getElementById('imageBox').src = "img/apple_" + total + ".png";
You need to add html in JavaScript like this:
<div id="foo"></div>
<script type="text/javascript">
var total = 4;
document.getElementById('foo').innerHTML = '<img src="img/apple_' + total + '.png" id="imageBox"/>';
</script>
window.onload = function() {
var total = 4;
document.getElementById('imageBox').src = 'img/apple_' + total + '.png"';
};
<img src="" id="imageBox"/>
var create_img_element = function(total, targetId){
//create the img element
var img = document.createElement('img');
//set the source of the image
img.src = 'img/apple_' + total + '.png';
//add the image to a specific element
document.getElementById(targetId).appendChild(img);
}
create_img_element(5, 'foo');