$(function(){
var $content = $(this).next();
$("#para h5.head").bind("mouseover",function(){
$content.show(); //$(this).next().show();
}).bind("mouseout",function(){
$content.hide(); //$(this).next().hide();
})
});
在執(zhí)行的時候义郑,變量 $content 訪問不到,如果按照注釋編輯代碼反而可以使用杈曲。
引用segmentfault上agui1989解答:
var $content = $(this).next();
//這里的$(this)
的上層如果找不到對象的話驰凛,會默認指向window,而window是沒有next()的担扑,這里就應該會報錯了洒嗤。
如果這里沒有報錯,那么這里的$content也只是特定的文檔對象魁亦,而不是你以為的$(this).next()
這段代碼渔隶。
而你bind里的$(this)指的是這個$("#para h5.head")
文檔對象。
如果你上邊的$content剛好也指向這個文檔的話洁奈,就不會報錯间唉,比如把var $content = $(this).next();
改成$("#para h5.head");
則bind里就可以這樣寫:$content.next().show();
如果var $content=$("#para h5.head").next();
則bind中可以這樣寫:$content.show();