$('.myshp_list_product_image img').each(function() {
tn_array.push($(this).attr('src'));
});
有了這段代碼,我把我的圖像src放在一個數組中。如何更改src的一部分並將其推入數組?
每一個形象,上面寫着「1」一部分像這樣的:「這 - 是 - 我的圖像,1s.jpg」
我想將「1」的部分更改爲「2秒」,之後推到陣列。
我該怎麼做?
$('.myshp_list_product_image img').each(function() {
tn_array.push($(this).attr('src'));
});
有了這段代碼,我把我的圖像src放在一個數組中。如何更改src的一部分並將其推入數組?
每一個形象,上面寫着「1」一部分像這樣的:「這 - 是 - 我的圖像,1s.jpg」
我想將「1」的部分更改爲「2秒」,之後推到陣列。
我該怎麼做?
使用String#replace
法2s
更換1s
雖然你可以使用map()
方法來生成陣列。
var tn_array = $('.myshp_list_product_image img').map(function() {
// return the updated attribute value
return $(this).attr('src').replace('1s', '2s');
// get the result as an array from jQuery object
}).get();
'$回報(本).attr( 'SRC')。取代(/ 1S /, '2S')'不'replace()'中的第一個參數需要包含在'slash'/'' –
@ Ashraf.Shk786中:這是用於正則表達式,,,這裏它可以是字符串,因爲我們只是想刪除第一個匹配項 –
U r welcome ..現在,它看起來不錯:-) –
使用替代
$('.myshp_list_product_image img').each(function() {
var src = $(this).attr('src');
tn_array.push(src.replace("1s.jpg", "2s.jpg"));
});
可以使用.replace在JS但可能是有BUG的一些圖片命名 –