Вот мой image tag
Это мой jquery, чтобы получить src
$('.hoverme').mouseover(function(){
console.log($(this).attr('src'));
});
Работает хорошо. Теперь на mouseover мне нужно изменить
src из
/images/image_107/thumb/Ubuntu-Wallpaper-HD.jpg?1380112803
в
/images/image_107/large/Ubuntu-Wallpaper-HD.jpg?1380112803
Как я могу это достичь.
Вы можете заменить
$('.hoverme').mouseover(function(){
console.log($(this).attr('src').replace("thumb", "large" ));
});
И если вы хотите убедиться, что его единственное, что заменяется, добавьте "/", как
$('.hoverme').mouseover(function(){
console.log($(this).attr('src').replace("/thumb/", "/large/" ));
});
Если вам нужно заменить, а затем вернуться
$('.hoverme').hover(function () {
$(this).attr('src', function (idx, src) {
return src.replace('/thumb/', '/large/')
})
}, function () {
$(this).attr('src', function (idx, src) {
return src.replace('/large/', '/thumb/')
})
});
Если это один из способов
$('.hoverme').mouseenter(function () {
$(this).attr('src', function (idx, src) {
return src.replace('/thumb/', '/large/')
})
});
Одним из решений является следующее:
HTML
<img src="/images/image_107/thumb/Ubuntu-Wallpaper-HD.jpg?1380112803" data-large="/images/image_107/large/Ubuntu-Wallpaper-HD.jpg?1380112803" />
JQuery
$('img').mouseover(
function()
{
var large = $(this).attr('data-large');
var small = $(this).attr('src');
$(this).attr('src', large);
$(this).attr('data-large', small);
}
);
// And for mouseout
$('img').mouseout(
function()
{
var large = $(this).attr('src');
var small = $(this).attr('data-large');
$(this).attr('src', large);
$(this).attr('data-large', small);
}
);
вы можете использовать замену
$('.hoverme').mouseover(function(){
var src = $(this).attr('src');
var newSrc = src.Replace("thumb", "large");
$(this).attr('src', newSrc);
});
попробуй это:
$('.hoverme').mouseover(function(){
this.src="/images/image_107/large/Ubuntu-Wallpaper-HD.jpg?1380112803"
});
Вы можете просто сделать:
$(this).attr('src').replace('/thumb/', '/large/');