我有这个密码..。
<script>
$(document).ready(function () {
$('.tabular .tab').prependTo($('.tabular'));
});
</script>我在这个html上使用的
<div class="tabular"> <a class="tab">Example 1</a>
<div class="tab_content">Ridiculus condimentum. Integer lacinia imperdiet felis morbi egestas dapibus
leo.</div> <a class="tab">Example 2</a>
<div class="tab_content">Auctor fames pede sem. Ullamcorper rhoncus pharetra purus pellentesque
nisi.</div> <a class="tab">Example 3</a>
<div class="tab_content">Lobortis hendrerit tellus maecenas pellentesque purus ante iaculis feugiat
nullam.</div>
</div>但是只有当我在一页上只有一个选项卡部分,如果我想在一页上有3个选项卡部分时,它才能工作,我必须重写它.
<script>
$(document).ready(function () {
$('.tabular-1 .tab-1').prependTo($('.tabular-1'));
$('.tabular-2 .tab-2').prependTo($('.tabular-2'));
$('.tabular-3 .tab-3').prependTo($('.tabular-3'));
});
</script>以及重写html和css。是否有重写第一个脚本的方法,这样我就不必每次添加选项卡部分时都要添加新的代码行?从jquery的角度来看,我认为它涉及添加索引和/或使用$(this),但我正在慢慢地了解它的位置。
发布于 2013-08-30 09:58:24
代码的问题是集合$('.tabular .tab')没有根据它们的父元素对它们进行分组,所以.prependTo('.tabular')将在页面上的最后一个.tabular之前移动所有的选项卡(文档顺序被执行)。
我最好的建议是使用.each()迭代每个父节点并移动它们的内部选项卡:
$('.tabular').each(function() {
$('.tab', this).prependTo(this);
});这使得标签与他们的父母“粘”在一起。
https://stackoverflow.com/questions/18530068
复制相似问题