2016-05-26 4 views
0

Ich habe folgende jQuery-Code.Teilung von Text in Jquery

innerHtml = innerHtml + '<div class="compare-box '+ $(this).attr("offertype") +'"> <div class="compare-close"><span class="closed" onclick="RemoveCompareOffer(\''+ $(this).attr("id") + '\')"><i class="fa fa-times"></i></span></div>' 
      + '<div class="compare-logo"><img src="' + $(this).attr("imagelink") + '" /></div><p>' + $(this).attr("offername") + '</p>' 
      + '<input type="button" value="View Details" class="viewbtn" onclick="CallOfferRenderAction(\'' + $(this).attr("offercode") + '\',\'' + $(this).attr("providercode") + '\')" /></div>'; 

Wenn $(this).attr("offertype") enthält special-box specialoffer ich teilen wollen und ersetzen Sie es mit specialoffer-compare. Wie kann ich das machen?

+0

was u teilen wollen und ersetzen mit dem String für besondere Box oder Special –

+0

I Spezial-Box entfernen möchten , halten Sie specialoffer und fügen Sie "-compare" an specialoffer –

+0

an, verwenden Sie eine Variable wie gezeigt ... 'var temp = $ (this) .attr (" offertype "); temp = temp.indexOf (" special-box specialoffer ")> -1? "specialoffer-compare": temp; innerHtml = innerHtml + ... + temp + .... ' –

Antwort

2

Verwenden Sie eine temporäre Variable und indexOf() zu überprüfen, ob ein String einen Teilstring enthält wie folgt: -

var temp = $(this).attr("offertype"); 
temp = temp.indexOf("special-box specialoffer") > -1 ? "specialoffer-compare" : temp; 

innerHtml = innerHtml + '<div class="compare-box '+ temp +'"> <div class="compare-close"><span class="closed" onclick="RemoveCompareOffer(\''+ $(this).attr("id") + '\')"><i class="fa fa-times"></i></span></div>' 
      + '<div class="compare-logo"><img src="' + $(this).attr("imagelink") + '" /></div><p>' + $(this).attr("offername") + '</p>' 
      + '<input type="button" value="View Details" class="viewbtn" onclick="CallOfferRenderAction(\'' + $(this).attr("offercode") + '\',\'' + $(this).attr("providercode") + '\')" /></div>';