开发者

list of links if the hrefs are the same replace the text jQuery

I have a list of links, some of which might be duplicate href's but the link text itself has been edited slightly.

At the minute, to get rid of the duplicates I have been using this code.

var seen = {};
 $('.sortable li div a').each(function() {
  var txt = $(this).text();
  var linkTxt = $(this).attr("href");
   if (seen[linkTxt]){
    $(this).parent().parent().remove();
    var sortableList = $(".sortable").html();
    $("#ItemDescription").val(sortableList);
   }else{
    seen[linkTxt] = true;
    $(".sortable li div").each(function(){
    var selectLength = $(this).find("input").length;
     if(selectLength == 0){
      $("<input type='checkbox' name='selected' class='select'/>").appendTo(this);
     }
 });

This code is just removing it, I need the code to replace the linkText at the top with the linkText nearer the bottom(newest goes here), then remove it, but only if the href's match.

Anyone any ideas?

my list looks like this

<ol class="sortable ui-sortable" style="float:right;"><li class="menu-item">
<div class="">Allure<input type="checkbox" name="selected" class="x_x_x_select"></div>
<ol class="">
    <li style="display: list-item;" class="">
    <div class=""><a href="/portfolios/energy-drink" class="" name="">Energy-Drink</a><input type="checkbox" name="selected" class="x_x_select"></div>
    </li>
</ol>
</li>
<li class="menu-item">
<div class="">Avon<input type="checkbox" name="selected" class="x_x_x_select"></div>
</li>
<li class="menu-item">
<div class="">Bitten SJP<input type="checkbox" name="selected" class="x_x_x_select"></div>
</li>
<li style="display: list-item;" class="">
    <div class=""><a href="/portfolios/energy-drink" class="" name="">Allure-Energy-Drink</a>&l开发者_运维问答t;input type="checkbox" name="selected" class="x_x_select"></div>
    </li>
</ol>


If I understood you correwctly, you want to take the item from the bottom, search for an according item on the top, replace that one's text and remove the element from the bottom.

var changed = true;
while(changed) {
    changed = false;
    $('.sortable li div a').each(function() {
        var all = $('.sortable li div a[href=' + $(this).attr('href') + ']');
        if(all.length > 1) {
            all.first().text(all.last().text());
            all.slice(1).remove();
            changed = true;
            return false;
        }
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜