我正在尝试检索用户点击的图像的图像源.这就是我所拥有的,它似乎没有起作用:
var imgSrc = getSrc(image)
function getSrc(image) {
$('img').each(function() {
$(this).click(function() {
$(this).attr('src')
}); //end click
}); //end each
} //end getSrc
Run Code Online (Sandbox Code Playgroud)
$('img').click(function() {
alert( $(this).attr('src') ); // or this.src
});
Run Code Online (Sandbox Code Playgroud)
你不需要任何循环.我认为上面的代码会很好用.
完整代码
function getSrc(image) {
var src;
$('img').click(function() {
src = $(this).attr('src'); // or this.src
});
return src;
}
Run Code Online (Sandbox Code Playgroud)
在您的问题中,您不在image代码中使用参数.我不确定你为什么要用它.如果你想使用你通过参数传递的get src,image那么你可以尝试:
$(image).attr('src');
Run Code Online (Sandbox Code Playgroud)