自动增加JS函数参数和目标中附加的类或ID号
Auto increment appended class or ID number in JS function parameter and target
我想知道如何修改一个函数,它需要一个类名+附加的数字,并应用于另一个类具有相应的数字。
我现在的位置是:
function hideSect()
{
if($('.trigger').is(":checked"))
$(".condition").hide();
else
$(".condition").show();
}
我如何修改它,使它可以动态地将trigger1应用于condition1,将trigger2应用于condition2,等等
相关HTML:
<input type="checkbox" class="trigger" onchange="hideSect()"><label><span>I do not wish to furnish this information.</span></label>
<div class="condition">
<!--Conditional content in here -->
</div>
有了这个HTML布局,你可以直接使用CSS
input.trigger:checked + label + div {
display: none;
}
<input type="checkbox" class="trigger" >
<label><span>I do not wish to furnish this information.</span>
</label>
<div class="condition">
<p>Hello</p>
<!--Conditional content in here -->
</div>
<br/>
<input type="checkbox" class="trigger" >
<label><span>I do not wish to furnish this information.</span>
</label>
<div class="condition">
<p>Hello</p>
<!--Conditional content in here -->
</div>
除此之外,你可以找到下一个具有类条件的兄弟。
$(".trigger").on("change", function(){
$(this).nextAll(".condition").first().toggle(!this.checked);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" class="trigger" >
<label><span>I do not wish to furnish this information.</span>
</label>
<div class="condition">
<p>Hello</p>
<!--Conditional content in here -->
</div>
<br/>
<input type="checkbox" class="trigger" >
<label><span>I do not wish to furnish this information.</span>
</label>
<div class="condition">
<p>Hello</p>
<!--Conditional content in here -->
</div>
如果不能依赖html布局,那么使用data属性
$(".trigger").on("change", function(){
var cb = $(this),
selector = cb.data("toggles");
$(selector).first().toggle(!this.checked);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" class="trigger" data-toggles="#f1" >
<label><span>I do not wish to furnish this information.</span>
</label>
<div class="condition" id="f1">
<p>Hello</p>
<!--Conditional content in here -->
</div>
<br/>
<input type="checkbox" class="trigger" data-toggles="#f2" >
<label><span>I do not wish to furnish this information.</span>
</label>
<div class="condition" id="f2">
<p>Hello</p>
<!--Conditional content in here -->
</div>
如果你真的需要使用id,你可以这样做
$(".trigger").on("change", function(){
var num = this.id.match(/'d+$/)[0];
$("#f" + num).toggle(!this.checked);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" class="trigger" ID="c1" >
<label><span>I do not wish to furnish this information.</span>
</label>
<div class="condition" id="f1">
<p>Hello</p>
<!--Conditional content in here -->
</div>
<br/>
<input type="checkbox" class="trigger" id="c2" >
<label><span>I do not wish to furnish this information.</span>
</label>
<div class="condition" id="f2">
<p>Hello</p>
<!--Conditional content in here -->
</div>
相关文章:
- Href:当前DIV中的目标ID
- 目标 ID 而不是
- 引导模式的目标 id,用于在模式关闭时停止播放视频
- 对于 JQuery 中具有不同事件的多个目标 ID 的相同函数
- 具有 ID AND 类的 jQuery 选择器目标元素不起作用
- 铬错误或编码错误?目标页面中的锚/ID 链接不起作用
- 具有不同 id 的目标元素 Jquery
- jQuery一次触发所有模态窗口,即使具有不同的目标 #id
- 目标类名,但使用唯一ID调用函数
- 获取已删除的元素id,而不是删除目标id
- 以动态id为目标's在javascript文件中
- 如何在ID更改时使用硒作为输入框的目标
- ASP.Net,控制ddl OnClientSelectedIndexChanged目标中的客户端ID
- 使用jquery以正确的id为目标
- Javascript中的目标输入字段;t没有类或ID
- 自动增加JS函数参数和目标中附加的类或ID号
- 只替换目标ID's中的字符串
- JQuery id目标与一个变量
- 如何动态设置数据目标id
- 如何在刷新后删除id(#)目标