2015-11-13 58 views
0

我有一個重複的容器,有幾個跨度元素。我想刪除兩個,並重新排列其中一個元素,如下例所示。我開始是這樣的:重新排列和移除容器內的元素

$('span.class5').insertBefore('span.class0'); 

這增加了每個重複容器內頁上找到每個span.class5,所以沒有工作..我如何做到這一點的最佳使用jQuery?

HTML:

<div class="container"> 

<div class="group"> 
    <span class="class0"></span> 
    <span class="class1"></span> 
    <span class="class2"></span> 
    <span class="class3"></span> 
    </div> 

    <div class="group"> 
    <span class="class4"></span> 
    <span class="class5"></span> 
    <span class="class6"></span> 
    <span class="class7"></span> 
    <span class="class8"></span> 
</div> 

</div> 

重排:

<div class="container"> 

<div class="group"> 
    <span class="class0"></span> 
    <span class="class5"></span> 
    <span class="class1"></span> 
    <span class="class2"></span> = remove 
    <span class="class3"></span> = remove 
    </div> 

    <div class="group"> 
    <span class="class4"></span> 
    <span class="class6"></span> 
    <span class="class7"></span> 
    <span class="class8"></span> 
</div> 

</div> 
+0

你什麼意思通過'= remove'做?他們只是需要刪除,或者=刪除部分HTML? –

+0

我需要刪除它們 - 這是更容易的部分 – user3344734

回答

2

的一個解決方案可能的:
(與jQuery)

// I would like to remove two 
 
$('.class2 , .class3' , '.group').remove(); 
 

 
//This added each span.class5 found on page inside each repeating container 
 
$('span.class5').each(function(){ 
 
    var $el = $(this); 
 
    var $father = $el.parents('.container'); 
 
    
 
    //   if you don't want to use a class for the container 
 
    //   decomment line under 
 
    //var $father = $el.parent().parent(); 
 
    
 
    
 
    $el.insertAfter($father.find('.class0')) 
 
});
span{ 
 
    border : solid 1px #EEE; 
 
    display : inline-block; 
 
    padding : 3px; 
 
    margin : 3px; 
 
    } 
 

 
.container{ 
 
    border : solid 1px #CCC; 
 
    padding 3px; 
 
    margin : 3px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class='container'> 
 
    <div class="group"> 
 
    <span class="class0">0</span> 
 
    <span class="class1">1</span> 
 
    <span class="class2">2</span> 
 
    <span class="class3">3</span> 
 
    </div> 
 

 
    <div class="group"> 
 
    <span class="class4">4</span> 
 
    <span class="class5">5</span> 
 
    <span class="class6">6</span> 
 
    <span class="class7">7</span> 
 
    <span class="class8">8</span> 
 
    </div> 
 
</div> 
 
<div class='container'> 
 
    <div class="group"> 
 
    <span class="class0">0</span> 
 
    <span class="class1">1</span> 
 
    <span class="class2">2</span> 
 
    <span class="class3">3</span> 
 
    </div> 
 

 
    <div class="group"> 
 
    <span class="class4">4</span> 
 
    <span class="class5">5</span> 
 
    <span class="class6">6</span> 
 
    <span class="class7">7</span> 
 
    <span class="class8">8</span> 
 
    </div> 
 
</div> 
 
<div class='container'> 
 
    <div class="group"> 
 
    <span class="class0">0</span> 
 
    <span class="class1">1</span> 
 
    <span class="class2">2</span> 
 
    <span class="class3">3</span> 
 
    </div> 
 

 
    <div class="group"> 
 
    <span class="class4">4</span> 
 
    <span class="class5">5</span> 
 
    <span class="class6">6</span> 
 
    <span class="class7">7</span> 
 
    <span class="class8">8</span> 
 
    </div> 
 
</div> 
 
<div class='container'> 
 
    <div class="group"> 
 
    <span class="class0">0</span> 
 
    <span class="class1">1</span> 
 
    <span class="class2">2</span> 
 
    <span class="class3">3</span> 
 
    </div> 
 

 
    <div class="group"> 
 
    <span class="class4">4</span> 
 
    <span class="class5">5</span> 
 
    <span class="class6">6</span> 
 
    <span class="class7">7</span> 
 
    <span class="class8">8</span> 
 
    </div> 
 
</div>

+0

這不起作用 - 我的頁面有10個重複容器,它將頁面上的class5元素的數量添加到每個容器。 – user3344734

+0

我修改了你的代碼! – Anonymous0day

+0

這很棒! – user3344734

0

你可以使用父>子選擇器來限制操作的特定容器:

function rearrange(num) { 
 
    $('.container' + num + ' > .group > .class2').remove(); 
 
    $('.container' + num + ' > .group > .class3').remove(); 
 
    $('.container' + num + ' > .group > .class5').insertBefore($('.container' + num + ' > .group > .class0')); 
 
}
.group { 
 
    background-color: lightgray; 
 
    margin: 4px 
 
} 
 
.group span { 
 
    margin: 3px 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container1"> 
 
    <div class="group"> 
 
    <span class="class0">0</span> 
 
    <span class="class1">1</span> 
 
    <span class="class2">2</span> 
 
    <span class="class3">3</span> 
 
    </div> 
 
    <div class="group"> 
 
    <span class="class4">4</span> 
 
    <span class="class5">5</span> 
 
    <span class="class6">6</span> 
 
    <span class="class7">7</span> 
 
    <span class="class8">8</span> 
 
    </div> 
 
    <button onclick="rearrange(1)">Rearrange</button> 
 
</div> 
 
<div class="container2"> 
 
    <div class="group"> 
 
    <span class="class0">0</span> 
 
    <span class="class1">1</span> 
 
    <span class="class2">2</span> 
 
    <span class="class3">3</span> 
 
    </div> 
 
    <div class="group"> 
 
    <span class="class4">4</span> 
 
    <span class="class5">5</span> 
 
    <span class="class6">6</span> 
 
    <span class="class7">7</span> 
 
    <span class="class8">8</span> 
 
    </div> 
 
    <button onclick="rearrange(2)">Rearrange</button> 
 
</div>

+0

問題是容器重複使用同一個類 - 我無法將其更改爲group1,group2等。 – user3344734

+0

好的,那麼如何識別最適合使用的容器呢?按容器的順序? –

+0

我希望這發生在所有的容器上,它們都是一樣的。但我有10個這樣的容器 - 這個代碼給了我每個容器內的10個class5元素。我希望移動只限於其父容器內發生。 – user3344734