【发布时间】:2015-01-28 04:46:51
【问题描述】:
我编写了一个 jquery 代码,它只是为 div 添加了一个样式。
$('input[type=radio][name=radio-set]').change(function() {
if (this.id == 'first') {
$("div.second, div.third").removeAttr('style');
$("div.first").css({"opacity":"1"});
}
else if (this.id == 'second') {
$("div.first, div.third").removeAttr('style');
$("div.second").css({"opacity":"1"});
}
else if (this.id == 'third') {
$("div.first, div.second").removeAttr('style');
$("div.third").css({"opacity":"1"});
}
});
<input id="first" type="radio" name="radio-set" checked="checked"/>
<input id="second" type="radio" name="radio-set" />
<input id="third" type="radio" name="radio-set" />
<div class="first">Test Text</div>
<div class="second">Test Text 2</div>
<div class="third">Test Text 3</div>
如何检查默认选中的单选按钮以在页面加载时向其相关 div 添加样式?!我在 jquery 代码的末尾添加了change(),但由于默认选中的属性位于带有#first id 的按钮上,因此它选择了带有.third 类的div。
【问题讨论】:
标签: javascript jquery html