我有这个代码,点击按钮后创建div。但它不起作用......这里是带有脚本的html
<img id="boxm" style="position: fixed;left: 90%;padding-top:2%"
src="images/box1.png" id="image1" onclick="diffImage(this);myFunction();" />
<script>
function myFunction()
{
document.getElementById("surprise").createElement="<img
style="width:304px;height:228px"
src='images/mese.png' />";
}
</script>
<div id="surprise">
</div>
答案 0 :(得分:1)
<img src='images/box1.png' onclick="showSurpriseImage()" />
<div id="surprise">
Surprise Image will be displayed here
</div>
<script>
function showSurpriseImage() {
var x = document.createElement("IMG");
x.setAttribute("src", "images/mese.png");
x.setAttribute("width", "304");
x.setAttribute("height", "228");
x.setAttribute("alt", "surprise image");
document.getElementById("surprise").appendChild(x);
}
</script>
设置属性的最佳做法checkout here
希望这有帮助
答案 1 :(得分:0)
<img id="boxm"
src="http://wfarm1.dataknet.com/static/resources/icons/set28/7f8535d7.png" id="image1" onclick="myFunction();" />
<script>
function myFunction() {
var s = document.getElementById("surprise")
var x = document.createElement("IMG");
x.setAttribute("src", "http://wfarm1.dataknet.com/static/resources/icons/set28/7f8535d7.png");
s.appendChild(x);
}
</script>
<div id="surprise">
</div>
&#13;
答案 2 :(得分:-1)
试试这个:
function myFunction()
{
document.getElementById("surprise").innerHTML='<img style="width:304px;height:228px" src="images/mese.png" />';
}