2014-02-19 54 views
0
$('.modal-left img').data('src').replace('-90x67',''); 

Uncaught TypeError: Cannot call method 'replace' of undefinedjQuery的刪除圖像源

的一部分使用上述代碼我試圖從所指定的圖像源除去-90x67。我無法使用它,有人可以協助嗎?

+0

什麼是HTML是什麼樣子?因爲代碼工程http://jsfiddle.net/JK7z3/ – epascarello

回答

1

嘗試.attr()

$('.modal-left img').attr('src').replace('-90x67',''); 


或者更好 .prop()

$('.modal-left img').prop('src').replace('-90x67',''); 


如果你想更換使用和設置更換 src

.prop(propertyName, value)

$('.modal-left img').prop('src',function(_,old){ 
    return old.replace('-90x67',''); 
}); 

.data()

0

這裏嘗試使用attr()

$('.modal-left img').attr('src').replace('-90x67',''); 

prop()代替:

$('.modal-left img').prop('src').replace('-90x67',''); 

你的代碼只能如果你把你的圖像源裏面data-src屬性

<img data-src="Your image src here" src="Your img src here" /> 
0

嘗試

var test=$('.modal-left img').attr('src').replace('-90x67',''); 

$('.modal-left img').attr('src',test); 
0

你可以操縱你的形象ATTR如下:

// To Change It 
$('#test img').attr('src', 'new value'); 

// To Remove It 
$('#test img').removeAttr('src'); 

例子: http://jsfiddle.net/AAyss/1/