2017-02-20 23 views
1
$('.myshp_list_product_image img').each(function() { 
    tn_array.push($(this).attr('src')); 
}); 

有了这段代码,我把我的图像src放在一个数组中。如何更改src的一部分并将其推入数组?

每一个形象,上面写着“1”一部分像这样的:“这 - 是 - 我的图像,1s.jpg”

我想将“1”的部分更改为“2秒”,之后推到阵列。

我该怎么做?

+0

可以使用.replace在JS但可能是有BUG的一些图片命名 –

回答

1

使用String#replace2s更换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(); 
+0

'$回报(本).attr( 'SRC')。取代(/ 1S /, '2S')'不'replace()'中的第一个参数需要包含在'slash'/'' –

+0

@ Ashraf.Shk786中:这是用于正则表达式,,,这里它可以是字符串,因为我们只是想删除第一个匹配项 –

+0

U r welcome ..现在,它看起来不错:-) –

1

使用替代

$('.myshp_list_product_image img').each(function() { 
    var src = $(this).attr('src'); 
    tn_array.push(src.replace("1s.jpg", "2s.jpg")); 
}); 
相关问题