Wow ..獲得關於'this'的真實信息是不是容易,因爲谷歌基本上忽略了這個詞。在嵌套函數中使用'this'
該代碼使用縮略圖中的信息打開數據庫中的圖像.. onlick的作品和懸停代碼的作品,但我不知道如何從mouseenter獲得'this'以用於showModal函數。
function showModal() {
$("body").css("overflow-y", "hidden");
$(".small").removeClass("smallHover");
$(".modal").fadeIn(200);
var altLong = $(this).attr("alt");
var altSplit = altLong.split("#");
$(".picTitle").text(altSplit[0]);
var srclong = $(this).attr("src");
var srcshort = srclong.split("_");
var srcextension = srclong.split(".");
$(".big").attr("src", srcshort[0]+'.'+srcextension[1]);
}
$(".small").click(showModal);
var timer;
$(".small").mouseenter(function() {
timer = setTimeout(function(){
$(this).showModal(); // **<--this is the line that doesnt work**
}, 2000);
}).mouseleave(function() {
clearTimeout(timer);
});
此外,如果你能解釋一下爲什麼要使用$(這)是一個jQuery對象,而不是僅僅「這個」以及它們如何不同,那將是巨大的。在此先感謝〜!
另外這個問題回答有助於整個問題OP正經歷:HTTPS: //sackoverflow.com/questions/5889237/jquery-nested-this-references –
Hooray!非常感謝你,我試着把它變成一個變量,然後用電話,但不是在一起。另外感謝您閱讀關於'this'的額外閱讀,它幫助我理解了爲什麼我的其他一些嘗試也無法正常工作(特別是試圖訪問屬性而不是在參考上執行函數) –