2016-05-26 3 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")は、私は分割しspecialoffer-compareと交換したいspecial-box specialoffer含まれています。これどうやってするの?

+0

何uは分割し、私は特別なボックスを削除する文字列の特別なボックスやspecialoffer –

+0

と交換したいですspecialofferを維持し、specialofferに '-compare'を追加します。 –

+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>'; 
関連する問題