$(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();