我的网站上有5行,每行都有一个图像.
单击时,图像的大小会增加,但是id会在目录前添加一个目录,然后它变成另一个图像.
这有可能与jQuery?
目前
<img src="image.jpg" />
Run Code Online (Sandbox Code Playgroud)
我想要的是
<img src="hires/image.jpg" />
Run Code Online (Sandbox Code Playgroud)
我读过关于使用'数据源'标签,但我似乎无法得到任何工作?
试着
$('img').click(function(){
$(this).attr({'src'}).prepend('hires');
});
Run Code Online (Sandbox Code Playgroud)
这是你想要的?
$('img').click(function(){
$(this).attr('src', function(i, value) {
return 'hires/' + value;
});
});
Run Code Online (Sandbox Code Playgroud)
它会优先hires/于每次点击.您可以使用.indexOf以下方法测试它是否已存在:
$(this).attr('src', function(i, value) {
return value.indexOf('hires/') === -1 ? 'hires/' + value : value;
});
Run Code Online (Sandbox Code Playgroud)
如果你想之间切换hires/,而不是hires/,删除字符串,如果它存在:
$(this).attr('src', function(i, value) {
return value.indexOf('hires/') === -1 ?
'hires/' + value :
value.replace('hires/', '');
});
Run Code Online (Sandbox Code Playgroud)
.prepend是一个添加DOM元素的jQuery方法.它不是本机字符串方法.那里的花括号也是错误的(语法错误).