我在调试这个脚本时遇到问题,最初的问题是“$()。ready不是函数”所以我把这部分内容包装好了:
$(document).ready(function()
{ $('#jsddm > li').bind('mouseover', jsddm_open);
$('#jsddm > li').bind('mouseout', jsddm_timer);});
document.onclick = jsddm_close;
像这样
( function($) {
$(document).ready( function() {
$('#menu-main-nav > li').bind('mouseover', jsddm_open);
$('#menu-main-nav > li').bind('mouseout', jsddm_timer);
});
document.onclick = jsddm_close;
} ) ( jQuery );
但我现在得到一个$(this).find不是“jsddm_close();”行上的函数 - 任何帮助表示赞赏,我确信这很简单。完整脚本如下:
<script type="text/javascript">
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;
function jsddm_open()
{ jsddm_canceltimer();
jsddm_close();
ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}
function jsddm_close()
{ if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}
function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout);}
function jsddm_canceltimer()
{ if(closetimer)
{ window.clearTimeout(closetimer);
closetimer = null;}}
( function($) {
$(document).ready( function() {
$('#menu-main-nav > li').bind('mouseover', jsddm_open);
$('#menu-main-nav > li').bind('mouseout', jsddm_timer);
});
document.onclick = jsddm_close;
} ) ( jQuery );
</script>
答案 0 :(得分:4)
你应该在一个匿名的函数中wra e ... ... ... <...
<script type="text/javascript">
(function($) {
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;
function jsddm_open() {
jsddm_canceltimer();
jsddm_close();
ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}
function jsddm_close() {
if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}
function jsddm_timer() {
closetimer = window.setTimeout(jsddm_close, timeout);
}
function jsddm_canceltimer() {
if (closetimer) {
window.clearTimeout(closetimer);
closetimer = null;
}
}
$(document).ready( function() {
$('#menu-main-nav > li').bind('mouseover', jsddm_open);
$('#menu-main-nav > li').bind('mouseout', jsddm_timer);
});
document.onclick = jsddm_close;
})(jQuery)
</script>