Javascript 我的幻灯片(jquery)菜单在跳,为什么?

Javascript 我的幻灯片(jquery)菜单在跳,为什么?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想创建一组div,如果我单击其中一个,其余的将使用jquery的slideToggle隐藏,但每次单击一个div(第一次除外),它都会上下滑动多次。这是我的密码: var-cntln=0; 函数生成(){ $.ajax({url:'xmlsample.xml',async:false,success:function(d){ $(d).查找('linea')。每个(函数(){ agregarlinea($(this.attr)(“nombre”); }); }}); } 功能性失调症(n)

我想创建一组div,如果我单击其中一个,其余的将使用jquery的slideToggle隐藏,但每次单击一个div(第一次除外),它都会上下滑动多次。这是我的密码:


var-cntln=0;
函数生成(){
$.ajax({url:'xmlsample.xml',async:false,success:function(d){
$(d).查找('linea')。每个(函数(){
agregarlinea($(this.attr)(“nombre”);
});
}});
}
功能性失调症(n){
var ndiv=document.createElement('div');
ndiv.id=“dv”+cntln;
ndiv.style.height=“100px”;
ndiv.style.border=“1px实体”;
ndiv.onmouseover=function(){darken(event.target.id)};
ndiv.onmouseout=function(){lighte(event.target.id)};
document.getElementsByTagName('body')[0].appendChild(ndiv);
cntln++;
}
函数变暗(e){
document.getElementById(e).style.backgroundColor=“#d3”;
document.body.style.cursor=“指针”;
var i;
$(“#”+e)。单击(函数(){

对于(i=0;i您正在
darken()
函数中绑定单击处理程序,因此它会被一次又一次地绑定。您应该将单击处理程序绑定到
agregarlena()
函数中

function agregarlinea(n) {
    var ndiv = document.createElement('div');
    ndiv.id = "dv" + cntln;
    ndiv.style.height = "100px";
    ndiv.style.border = "1px solid";
    ndiv.onmouseover = function () {
        darken(event.target.id)
    };
    ndiv.onmouseout = function () {
        lighten(event.target.id)
    };
    $(ndiv).click(function () {
        var thisId = $(this).attr('id');
        for (var i = 0; i <= cntln; i++) {
            if (("dv" + i) != thisId) {
                $("#dv" + i).slideToggle("fast", "swing");
            }
        }
    });
    document.getElementsByTagName('body')[0].appendChild(ndiv);
    cntln++;
}

function darken(e) {
    document.getElementById(e).style.backgroundColor = "#D3D3D3";
    document.body.style.cursor = "pointer";
}
函数agregarlena(n){
var ndiv=document.createElement('div');
ndiv.id=“dv”+cntln;
ndiv.style.height=“100px”;
ndiv.style.border=“1px实体”;
ndiv.onmouseover=函数(){
变暗(event.target.id)
};
ndiv.onmouseout=函数(){
减轻(event.target.id)
};
$(ndiv)。单击(函数(){
var thisId=$(this.attr('id');

对于(var i=0;i您正在
darken()
函数中绑定单击处理程序,因此它会被一次又一次地绑定。您应该在
agregarlena()
函数中绑定单击处理程序

function agregarlinea(n) {
    var ndiv = document.createElement('div');
    ndiv.id = "dv" + cntln;
    ndiv.style.height = "100px";
    ndiv.style.border = "1px solid";
    ndiv.onmouseover = function () {
        darken(event.target.id)
    };
    ndiv.onmouseout = function () {
        lighten(event.target.id)
    };
    $(ndiv).click(function () {
        var thisId = $(this).attr('id');
        for (var i = 0; i <= cntln; i++) {
            if (("dv" + i) != thisId) {
                $("#dv" + i).slideToggle("fast", "swing");
            }
        }
    });
    document.getElementsByTagName('body')[0].appendChild(ndiv);
    cntln++;
}

function darken(e) {
    document.getElementById(e).style.backgroundColor = "#D3D3D3";
    document.body.style.cursor = "pointer";
}
函数agregarlena(n){
var ndiv=document.createElement('div');
ndiv.id=“dv”+cntln;
ndiv.style.height=“100px”;
ndiv.style.border=“1px实体”;
ndiv.onmouseover=函数(){
变暗(event.target.id)
};
ndiv.onmouseout=函数(){
减轻(event.target.id)
};
$(ndiv)。单击(函数(){
var thisId=$(this.attr('id');

对于(var i=0;i@Matthew-还有什么地方比创建div后绑定单击处理程序更好?@Matthew-还有什么地方比创建div后绑定单击处理程序更好?@Matthew-还有什么地方比创建div后绑定单击处理程序更好?