我试图让我的代码在同一个窗口中执行我的函数,但它总是用一个仅运行该函数的新窗口覆盖当前窗口,当然这会使我的程序完全停止。如何更改我的代码以便它在同一窗口中打开? (Java 新手)
<button onclick="blackFemale()">Black Female</button>
<script>
document.write("Click the button of the character you would like to be, then click play.");
function blackFemale() {
character = "black female";
document.write("You are a ", character, ".");
var x = document.createElement("IMG");
x.setAttribute("src", "IntroImages/FemaleBlack.jpg");
x.setAttribute("width", "304");
x.setAttribute("width", "228");
x.setAttribute("alt", "black female");
document.body.appendChild(x);
}
帮助将不胜感激,由于新手,我在问这个问题时非常紧张,哈哈。
最佳答案
document.write
覆盖整个页面。您可以将其附加到正文。
<button onclick="blackFemale()">Black Female</button>
js:
function blackFemale() {
character = "black female";
var text =document.createTextNode("You are a " + character + ".");
document.body.appendChild(text);
var x = document.createElement("IMG");
x.setAttribute("src", "IntroImages/FemaleBlack.jpg");
x.setAttribute("width", "304");
x.setAttribute("width", "228");
x.setAttribute("alt", "black female");
document.body.appendChild(x);
}
关于javascript - 为什么我的按钮使我的函数在不同的窗口中打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016204/