当单击一个特定的div时,我希望另一个div创建一个带有文本的新p样式。
创建的第一个函数可以正常工作-无论第二个函数似乎否决第一个函数。我尝试过更改几乎所有内容的名称,但它似乎仍然无效。我该如何解决?
var para = document.createElement("P");
para.innerHTML = "The Siamese crocodiles share one stomach, yet they fight over food.";
document.getElementById("information").appendChild(para);
}
window.onload = function() {
document.getElementById("i29").ondblclick = function i29i() {
i29();
}
}
function i3() {
var para = document.createElement("P");
para.innerHTML = "When you climb a good tree, you are given a push.";
document.getElementById("information").appendChild(para);
}
window.onload = function() {
document.getElementById("i3").ondblclick = function i3i() {
i3();
}
}```
答案 0 :(得分:1)
您可以这样做:
HTML:
<div onClick="firstClick()">First</div>
<div onClick="secondClick()">Second</div>
<div id="information"></div>
JS:
function firstClick(){
var para1 = document.createElement("P");
para1.innerHTML = "The Siamese crocodiles share one stomach, yet they fight over food.";
document.getElementById("information").appendChild(para1);
}
function secondClick(){
var para2 = document.createElement("P");
para2.innerHTML = "When you climb a good tree, you are given a push.";
document.getElementById("information").appendChild(para2);
}
答案 1 :(得分:1)
您正在将window.onload重置为另一个值,当您两次将其分配给其他功能时。最简单的解决方案是执行以下操作:
window.onload = function() {
document.getElementById("i29").ondblclick = i29;
document.getElementById("i3").ondblclick = i3;
}