这是我的问题:
$("#gallery > img").live('click',function() {
$(this).prev().css("background" , "#f99"); // WORKS !
var src = $(this).prev().src; // DOESN'T WORK (src is undefined)
});
Run Code Online (Sandbox Code Playgroud)
我在google chrome调试器中看到prev()函数返回一个jQuery.fn.jQuery.init [1]对象...它似乎包含我希望在索引0处使用prev()的HTMLImageElement,但是使用它就像数组不起作用.
我迷路了,我可以帮忙...谢谢你们
// get the DOM element and access the src property
var src = $(this).prev()[0].src;
Run Code Online (Sandbox Code Playgroud)
要么:
// get the DOM element and access the src property
var src = $(this).prev().get(0).src;
Run Code Online (Sandbox Code Playgroud)
要么:
// access the src via .attr()
var src = $(this).prev().attr("src");
Run Code Online (Sandbox Code Playgroud)