gpt4 book ai didi

javascript - 通过按下下拉按钮更改段落中的文本

转载 作者:行者123 更新时间:2023-12-04 07:29:51 25 4
gpt4 key购买 nike

我试图通过单击下拉按钮将段落标记中的文本从“hi”更改为“U”,但它似乎不起作用......这是我的代码:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>ID</title>
</head>

<body>

<style type="text/css">
body {
background-color: lightgray;
font-size: 20px;
}

.dropbtn {
position: relative;
background-color: black;
color: white;
top: 1px;
font-size: 16px;
border: none;
}

.dropdown {
position: relative;
display: inline-block;
}

.dropdown-content {
position: relative;
display: none;
position: absolute;
z-index: 1;
top: 20px;
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

#hi {
position: relative;
left: 500px;
}

.dropdown-content a:hover {
background-color: #ddd;
}

.dropdown:hover .dropdown-content {
display: block;
}

.dropdown:hover .dropbtn {
background-color: black;
}
</style>
<div class="dropdown">
<button class="dropbtn">Language</button>
<div class="dropdown-content">
<button id="U" onclick="change()">U=NFPA (USA)</button>
</div>
</div>
<p id="hi">hi</p>

<script type="text/javascript">
function change() {
document.getElementById("hi").value = "U";
}
</script>

</body>

</html>

最佳答案

没有value HTMLHeadingElement 的属性(property).您应该使用 innerHTMLtextContent :

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>ID</title>
</head>

<body>

<style type="text/css">
body {
background-color: lightgray;
font-size: 20px;
}

.dropbtn {
position: relative;
background-color: black;
color: white;
top: 1px;
font-size: 16px;
border: none;
}

.dropdown {
position: relative;
display: inline-block;
}

.dropdown-content {
position: relative;
display: none;
position: absolute;
z-index: 1;
top: 20px;
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}

#hi {
position: relative;
left: 500px;
}

.dropdown-content a:hover {
background-color: #ddd;
}

.dropdown:hover .dropdown-content {
display: block;
}

.dropdown:hover .dropbtn {
background-color: black;
}
</style>
<div class="dropdown">
<button class="dropbtn">Language</button>
<div class="dropdown-content">
<button id="U" onclick="change()">U=NFPA (USA)</button>
</div>
</div>
<p id="hi">hi</p>

<script type="text/javascript">
function change() {
document.getElementById("hi").textContent = "U";
}
</script>

</body>

</html>

关于javascript - 通过按下下拉按钮更改段落中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68005469/

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