Javascript jquery-on hover li width id-显示匹配的id div

Javascript jquery-on hover li width id-显示匹配的id div,javascript,jquery,Javascript,Jquery,您使用了错误的选择器。另外,“#new-”+this.id+“此语法错误。不需要在字符串中添加那些双引号 jQuery('.level0 li').hover(函数(){ var cat='#new-'+this.id; jQuery(cat.show(); },函数(){ var cat='#new-'+this.id; jQuery(cat.hide(); }); .alles zwei{ 显示:无; } cat2441 cat2450 cat2455 cat2441 cat2450

您使用了错误的选择器。另外,
“#new-”+this.id+“
此语法错误。不需要在字符串中添加那些双引号

jQuery('.level0 li').hover(函数(){
var cat='#new-'+this.id;
jQuery(cat.show();
},函数(){
var cat='#new-'+this.id;
jQuery(cat.hide();
});
.alles zwei{
显示:无;
}

    cat2441 cat2450 cat2455
cat2441 cat2450
cat2455
您不必依赖元素的id即可完成此操作,只需使用两个元素的类即可。当您选择类时,它将作为数组返回,这样您就可以将level1类数组与alles zwei类数组进行匹配。它还将简化您的HTML代码

jQuery('.alles li').mouseover(function() {
    var cat = '"#new-' + this.id + '"';
    jQuery(cat).fadeIn();
});

谢谢,就这样。当将鼠标悬停在li元素上时,会出现延迟(在DIV上显示)。这可以解决吗?@ibu400如果您使用
show/hide
而不是
fadeIn/fadeOut
,您可以更改它。请参阅更新的答案。
jQuery('.alles li').mouseover(function() {
    var cat = '"#new-' + this.id + '"';
    jQuery(cat).fadeIn();
});
$('.level1').hover(function(){
    // Gets the index of the current li emement.
    var indx = $(this).index(); 

    // Gets the div element based on the hovered li and hides its siblings.
    $('.alles-zwei').eq(indx).show().siblings('div').hide();
});