gpt4 book ai didi

javascript - 设置默认单选按钮 + 关联的 Div

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:48 25 4
gpt4 key购买 nike

我正在创建一个小部件,该小部件基于贵金属商品的报价,利用程式化的 radio 和 div 来显示不同的数据位。我需要在页面加载时默认加载 radio “当前”。使用 checked="checked" 会检查启用 radio (通过在标签下划线),但它不会加载关联的 div。我也需要默认加载 div。

HTML:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="style.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function() {
$("[name=toggler]").click(function(){
$('.toHide').hide();
$("#blk-"+$(this).val()).show(100);
});
});
</script>
</head>
<body>
<div id="title">
<text id="titletext">Commodities</text>
</div>
<div id="titleaccent">
</div>
<!--The form for the radio toggle buttons-->
<form name="range">
<input id="rdb1" class="radio" type="radio" name="toggler" value="1" >
<label for="rdb1">Current</label>
<input id="rdb2" class="radio" type="radio" name="toggler" value="2" >
<label for="rdb2">24-Hour</label>
<input id="rdb3" class="radio" type="radio" name="toggler" value="3" >
<label for="rdb3">Week</label>
<input id="rdb4" class="radio" type="radio" name="toggler" value="4" >
<label for="rdb4">Month</label>
</form>

<!--The div and associated data for each radio toggle button-->
<div id="blk-1" class="toHide" style="display:none">
Current commodity quote info here
</div>
<div id="blk-2" class="toHide" style="display:none">
24-hour comparison of commodity quote info here
</div>
<div id="blk-3" class="toHide" style="display:none">
Weekly comparison of commodity quote info here
</div>
<div id="blk-4" class="toHide" style="display:none">
Monthly comparison of commodity quote info here
</div>
</body>
</html>

CSS:

#title{
background-color: #103346;
height: 28px;
width: 305px;
}

#titletext{
color: #ffffff;
font-size: 15pt;
position: fixed;
top: 11px;
left: 18px;
}

#titleaccent{
background-color: #C3A43F;
height: 6px;
width: 305px;
}

.radio{
display: none;
margin: 10px;
}

.radio + label{
display:inline-block;
margin:-2px;
padding: 4px 12px;
}

.radio:checked + label{
text-decoration: underline;
}

最佳答案

使用一些 CSSJS:

http://jsfiddle.net/cW4DR/1/

$(function() {
$("[name=toggler]").click(function(){
$('.toHide').hide();
$('.current').hide();
$("#blk-"+$(this).val()).show(100);
});
});

.toHide{
display:none;
}
.current{
display:block;
}

关于javascript - 设置默认单选按钮 + 关联的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21057224/

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