我想添加一個「粉紅色」跨度類到字符串的末尾。針對每個類,並返回一個字符串和類
我可以得到這個與一個元素一起工作,但是當頁面加載多個時,它只會返回第一個元素。
我創建了一個jSFiddle
在此的jsfiddle是兩個頭銜:
20 Smithsons街
25瓊斯街
但它只是返回20 Smithsons街。
他們有什麼辦法可以針對每個元素?
我jQuery是如下:
// Style The Last Element In The Property h3 string
function change_colour()
{
var property_title = $('.property-title').html();
var lastWord = property_title.substring(property_title.lastIndexOf(" ")+1);
var lastIndex = property_title.lastIndexOf(" ")
var property_title = property_title.substring(0, lastIndex);
var style = '<span class="pink"> '+lastWord+'</span>';
return property_title + style;
}
$(".property-title").each(function() {
$('.property-title').html(change_colour());
});
感謝
你不需要'.each()' – Andreas
@Andreas你當然是對的。 – Tibos