gpt4 book ai didi

css - 单选按钮导致浏览器跳转到顶部

转载 作者:技术小花猫 更新时间:2023-10-29 10:39:29 24 4
gpt4 key购买 nike

问题:

单击标签时(对于通过将其放置在屏幕外而有意隐藏的单选按钮),浏览器意外跳转到页面顶部。

注意:这个问题在不同的浏览器之间是不一致的 -- 它出现在 safari 和 chrome 中,它不会出现在 firefox 或 opera 中


问题:

如何防止浏览器在单击单选按钮标签时将滚动条跳到页面顶部?


示例代码:

JS Fiddle

• HTML

<div class="rdbut">
<label class="colour">
<input id="option-AVGANT1Y2PC" type="radio" name="Multi-licence" value="1 Year 2 PC|+23.99|0" checked="checked" />
<span>£24.99</span></label>
</div>
<div class="rdbut">
<label class="colour">
<input id="option-AVGANT2Y2PC" type="radio" name="Multi-licence" value="2 Year 2 PC|+34.00|0" checked="checked" />
<span>£35.00</span></label>
</div>

• CSS

.rdbut {
margin: 0px;
}

.rdbut label {
float: left;
width: 65px;
margin: 4px;
background-color: #EFEFEF;
box-shadow: 0px 1px 3px rgba(50, 50, 50, 0.25);
border: none;
overflow: auto;
display: inline;
}

.rdbut label span {
text-align: center;
font-size: 12px;
padding: 3px 8px;
display: block;
}

.rdbut label input {
position: absolute;
top: -9999px;
left: -9999px;
}

.rdbut input:checked+span {
background-color: #404040;
color: #F7F7F7;
}

.rdbut .colour {
background-color: #FF8E22;
color: #ffffff;
}

最佳答案

很有趣。我不知道它为什么会这样,但是解决不需要的行为很容易:将 radio 输入的 CSS topleft 值交换为 visibility : 隐藏

像这样:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<style>
#content {
width: 500px;
}
.rdbut {
margin:0px;
}
.rdbut label {
float:left;
width:65px;
margin:4px;
background-color:#EFEFEF;
border:none;
overflow:auto;
display:inline;
}
.rdbut label span {
text-align:center;
font-size: 12px;
padding:3px 8px;
display:block;
}
.rdbut label input {
position:absolute;
t/op: -9999px;
l/eft: -9999px;
visibility: hidden;
}
.rdbut input:checked + span {
background-color:#404040;
color:#F7F7F7;
}
.rdbut .colour {
background-color:#FF8E22;
color:#ffffff;
}
</style>
</head>
<body>
<div id="content">
<p>"Lorem ipsum" goes here.
</p>
<div class="rdbut">
<label class="colour"><input id="option-AVGANT1Y2PC" type="radio" name="Multi-licence" value="1 Year 2 PC|+23.99|0" checked="checked" />
<span>£24.99</span></label>
</div>
<div class="rdbut">
<label class="colour"><input id="option-AVGANT2Y2PC" type="radio" name="Multi-licence" value="2 Year 2 PC|+34.00|0" checked="checked" />
<span>£35.00</span></label>
</div>
<div class="rdbut">
<label class="colour"><input id="option-AVGANT1Y2PC" type="radio" name="Multi-licence" value="1 Year 2 PC|+23.99|0" checked="checked" />
<span>£24.99</span></label>
</div>
</div>
</body>
</html>

在此处更新了 JSFiddle:http://jsfiddle.net/XkQ7T/1/ .

.
顺便说一句,在每个单选按钮上设置 checked 是没有用的——实际上只有最后一个被选中。它会使您的代码无效。此外,您需要在 radio 输入组周围使用 form 标记。

关于css - 单选按钮导致浏览器跳转到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24299567/

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