gpt4 book ai didi

javascript - 按下按钮时下拉 div

转载 作者:行者123 更新时间:2023-11-28 12:54:29 25 4
gpt4 key购买 nike

我目前有一个与 javascript 代码链接的 HTML 页面,它允许我按下一个按钮来显示隐藏的 div 中的内容。该功能工作正常,但是当按下按钮时,隐藏的 div 显示在按钮上方。无论如何它可以下降到按钮下方而不是下降。这是我正在使用的代码:

HTML:

<div id="spoiler1" style="display:none">
<p>1. Climb a tree<input type="checkbox"></p>
<p>2. Roll down a really big hill<input type="checkbox" ></p>
<p>3. Camp out in the wild<input type="checkbox" ></p>
<p>4. Build a den<input type="checkbox" ></p>
<p>5. Skim a stone<input type="checkbox" ></p>
<p>6. Run around in the rain<input type="checkbox" ></p>
<p>7. Fly a kte<input type="checkbox" ></p>
<p>8. Catch a fish with a net<input type="checkbox" ></p>
<p>9. Eat an apple straight from a tree<input type="checkbox" ></p>
<p>10. Play conkers<input type="checkbox" ></p>

</div>

<button id="button1" title="Click to show/hide content" type="button1" onclick="readmore1()">Adventurer &#x25BC;</button>
<div id="spoiler1" style="display:none">spoiler text</div>

Javascript:

function readmore1(){
var spoiler1 = document.getElementById('spoiler1');
var btn = document.getElementById('button1');


if(spoiler1.style.display=='none') {
spoiler1.style.display = '';
btn.innerHTML = "Adventurer &#x25B2;";
} else {
spoiler1.style.display = 'none';
btn.innerHTML = "Adventurer &#x25BC;";
}
}

任何帮助将不胜感激。

CSS:

#button1 {

display: inline-block;
font: normal 1.5em optima;
line-height: 10px;
margin-left: -10px;
margin-bottom:20px;
width: 250px;
height:60px;
border-radius:8px;
text-align: center;
vertical-align: central;
color: #fff;
border: none;
position: relative;
top: -5px;
left: 30px;

最佳答案

嘿伙计,我已经更详细地查看了您的代码,这里有一个代码示例,我将您的 Javascript 更新为 Jquery,因为它是一种更强大的语言:)

<!DOCTYPE html>
<html>
<head>
<script>

$(document).ready(function(){
$("#spoiler1").hide();
$("#button1").click(function(){
$("#spoiler1").show();
});

$("#button").click(function(){
$("#spoiler1").hide();
});

});
</script>
</head>
<body>
<button id="button1" title="Click to show/hide content" type="button1">Adventurer &#x25BC; </button>
<button id="button" title="Click to show/hide content" type="button">Hide</button>
<div id="spoiler1" style="display:none">
<p>1. Climb a tree
<input type="checkbox">
</p>
<p>2. Roll down a really big hill
<input type="checkbox">
</p>
<p>3. Camp out in the wild
<input type="checkbox">
</p>
<p>4. Build a den
<input type="checkbox">
</p>
<p>5. Skim a stone
<input type="checkbox">
</p>
<p>6. Run around in the rain
<input type="checkbox">
</p>
<p>7. Fly a kte
<input type="checkbox">
</p>
<p>8. Catch a fish with a net
<input type="checkbox">
</p>
<p>9. Eat an apple straight from a tree
<input type="checkbox">
</p>
<p>10. Play conkers
<input type="checkbox">
</p>
</div>
</body>
</html>

关于javascript - 按下按钮时下拉 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22841816/

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