2016-05-26 2 views
0

У меня есть следующий код jQuery.разделительный текст в 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>'; 

Всякий раз, когда $(this).attr("offertype") содержит special-box specialoffer Я хочу, чтобы разделить и заменить его specialoffer-compare. Как я могу это сделать?

+0

, что и хотят разделить и заменить, с которой струна специальный ящик или specialoffer –

+0

Я хочу, чтобы удалить специальный ящик , сохраняйте специальное предложение и добавляйте '-compare' к specialoffer –

+0

использовать переменную, как показано ...' var temp = $ (this) .attr ("offertype"); temp = temp.indexOf ("special-box specialoffer")> -1? «specialoffer-compare»: temp; innerHtml = innerHtml + ... + temp + .... ' –

ответ

2

Используйте временную переменную и indexOf(), чтобы проверить, если строка содержит подстроку, как показано ниже: -

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>'; 
Смежные вопросы