2016-12-16 65 views
0

我想更改dom中最后添加元素的背景。在这里给你一个更好看的是HTML:更改动态添加元素的背景

<div class="check-in-wrapper"> 
     <div class="ui-grid-a"> 
     <div class="ui-block-a"> 
      <span class="ticket-img"> 
       <img class="image" src="img/icons/ticket.png"> 
      </span> 
     </div> 

     <div class="ui-block-b"> 
      <h4>Inchecken</h4> 
      <div class="check-in-notification-append"></div> 
     </div> 
     </div> 
    </div> 

    <div class="douane-wrapper"> 
     <div class="ui-grid-a"> 
     <div class="ui-block-a"> 
     <span class="douane-img"> 
      <img class="douane-img image" src="img/icons/douane.png"> 
      </span> 
     </div> 

      <div class="ui-block-b"> 
      <h4>Douane</h4> 
      <div class="douane-notification-append"></div> 
     </div> 
     </div> 
    </div> 

    <div class="gate-wrapper"> 
    <div class="ui-grid-a"> 
    <div class="ui-block-a"> 
     <span class="gate-img"> 
     <img class="gate-img image" src="img/icons/gate.png"> 
     </span> 
    </div> 

    <div class="ui-block-b"> 
    <h4>Gate</h4> 
     <div class="gate-notification-append"></div> 
     </div> 
    </div> 

我在哪里的元素追加到check-in-notification-appenddouane-in-notification-appendgate-in-notification-append在此间举行的js文件更好看。

$(document).on("pagebeforeshow","#progress",function(){ 

    var incheckHtml = ''; 
    var douaneHtml = ''; 
    var gateHtml = ''; 

    for(var count = 0; count < info.data.length; count++){ 
     var shortmessage = info.data[count][3]; 
     var category = info.data[count][4]; 
     var typeOf = info.data[count][5]; 

     if(typeOf === "warning"){ 
      imgPath = 'img/icons/alarm.png'; 
     } else { 
      imgPath = 'img/icons/alert.png'; 
     } 


     if (!Array.prototype.last){ 
      Array.prototype.last = function(){ 
       return this[this.length - 1]; 
      }; 
     }; 


     if (category === 'inchecken'){ 
      incheckHtml = incheckHtml + "<div class='notification-item'><img class='notification-image' src=" + imgPath + "><span class='notification-text'>" + shortmessage + "</span></div>"; 
      // $('.check-in-wrapper').empty().prepend(incheckHtml); 
      $('.check-in-notification-append').empty().prepend(incheckHtml); 
     } 

     if(category === 'douane'){ 
      douaneHtml = douaneHtml + "<div class='notification-item overlay'><img class='notification-image' src=" + imgPath + "><span class='notification-text'>" + shortmessage + "</span></div>"; 
      $('.douane-notification-append').empty().prepend(douaneHtml); 
     } 

     if(category === 'gate'){ 
      gateHtml = gateHtml + "<div class='notification-item overlay'><img class='notification-image' src=" + imgPath + "><span class='notification-text'>" + shortmessage + "</span></div>"; 
      $('.gate-notification-append').empty().prepend(gateHtml); 
     } 
    } 
}); 

但我怎么存取权限的最后添加元素的所有类别,它消除了阶级"overlay"。我希望有人能够帮助我访问最后添加的元素和removeClass覆盖。我写了一个.last() function但这只会给我阵列中最后一个对象的输出...

回答

1

我认为你可以通过使用一些通用的选择器来适应所有可能受到影响的元素并使用Jquery的last()函数。

$(".allMyThings").last().removeClass("overlay"); 

https://api.jquery.com/last/

+0

如果我不喜欢这样写道:'$( '通知项。 ')最后()removeClass(' 覆盖');'比它会从所有最后删除类。在每个部分。但是我怎么才能从最后一个中删除这个类? – Sreinieren