gpt4 book ai didi

javascript - 更改段落中的按钮元素

转载 作者:行者123 更新时间:2023-12-03 04:54:06 25 4
gpt4 key购买 nike

/*jshint strict:false */
document.getElementById("btn_1").addEventListener("click", function() {
test(this);
});
document.getElementById("btn_2").addEventListener("click", function() {
test(this);
});
document.getElementById("btn_3").addEventListener("click", function() {
test(this);
});

function test(id) {
var e = document.getElementById(id);
var d = document.createElement('p');
d.innerHTML = e.innerHTML;
e.parentNode.insertBefore(d, e);
e.parentNode.removeChild(e);
}
/* CSS Document */

body {
background-color: #484848;
}

p {
margin: 2em 2em;
color: aliceblue;
}

button {
margin: 1em 2em;
color: ghostwhite;
background: #677762;
}
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">

<title>Line Game</title>
</head>
<p>Als die ersten Menschen auftauchten lehrten wir sie:</p>
<button id="btn_1">Jagen</button>
<button id="btn_2">Fischen</button>
<button id="btn_3">Landwirtschaft</button>
<!--Javascript Time-->
<script src="Javascript.js"></script>

<body>
</body>

</html>

这是我的所有代码。我想在有人单击按钮后将按钮替换为段落。按钮中的旧文本应该仍然存在。

是的,对转储问题感到抱歉,尝试了几个小时,对尝试和错误感到有点厌倦。

最佳答案

test(this); 更改为 test(this.id);

/*jshint strict:false */
document.getElementById("btn_1").addEventListener("click", function() {
test(this.id);
});
document.getElementById("btn_2").addEventListener("click", function() {
test(this.id);
});
document.getElementById("btn_3").addEventListener("click", function() {
test(this.id);
});

function test(id) {
var e = document.getElementById(id);
var d = document.createElement('p');
d.innerHTML = e.innerHTML;
e.parentNode.insertBefore(d, e);
e.parentNode.removeChild(e);
}
/* CSS Document */

body {
background-color: #484848;
}

p {
margin: 2em 2em;
color: aliceblue;
}

button {
margin: 1em 2em;
color: ghostwhite;
background: #677762;
}
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">

<title>Line Game</title>
</head>
<p>Als die ersten Menschen auftauchten lehrten wir sie:</p>
<button id="btn_1">Jagen</button>
<button id="btn_2">Fischen</button>
<button id="btn_3">Landwirtschaft</button>
<!--Javascript Time-->
<script src="Javascript.js"></script>

<body>
</body>

</html>

关于javascript - 更改段落中的按钮元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42510934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com