这是我的问题:
$("#gallery > img").live('click',function() {
$(this).prev().css("background" , "#f99"); // WORKS !
var src = $(this).prev().src; // DOESN'T WORK (src is undefined)
});
我在 google chrome 调试器中看到 prev() 函数返回一个 jQuery.fn.jQuery.init[1] 对象...它似乎包含我想要的 HTMLImageElement,prev() 在索引 0 处,但工作正常像数组一样使用它不起作用。
我在这里迷路了,我需要一些帮助......谢谢你们
最佳答案
// get the DOM element and access the src property
var src = $(this).prev()[0].src;
或者:
// get the DOM element and access the src property
var src = $(this).prev().get(0).src;
或者:
// access the src via .attr()
var src = $(this).prev().attr("src");
关于jQuery : get previous image with prev(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9038184/