Javascript 要基于值在jquery中创建循环吗

Javascript 要基于值在jquery中创建循环吗,javascript,jquery,Javascript,Jquery,我试图为mouseenter函数创建一个循环,以便为动态打印的任意多个div创建循环。div的计数位于id为计数器的隐藏文本框中。请帮我做这个循环。假设您的标记如下所示,每个函数对我都不起作用: var cnt = document.getElementById('counter'); //row1 is incremental and based on some value from // database this value will increase. want to creat

我试图为mouseenter函数创建一个循环,以便为动态打印的任意多个div创建循环。div的计数位于id为计数器的隐藏文本框中。请帮我做这个循环。假设您的标记如下所示,每个函数对我都不起作用:

 var cnt = document.getElementById('counter');
 //row1 is incremental and based on some value from 
 // database this value will increase. want to create a loop Here

$(".row1").mouseenter(function(){
    $(this).addClass("answer_hover_row");
    document.getElementById('row1').style.visibility = "visible";
}).mouseleave(function(){
    $(this).removeClass("answer_hover_row");
    document.getElementById('row1').style.visibility = "hidden";
});

$(".row2").mouseenter(function(){
    $(this).addClass("answer_hover_row");
    document.getElementById('row2').style.visibility = "visible";
}).mouseleave(function(){
    $(this).removeClass("answer_hover_row");
    document.getElementById('row2').style.visibility = "hidden";
});
var cnt = parseInt(document.getElementById('counter').innerHTML, 10);
for (var i = 1; i <= cnt; i++) {
    $(".row" + i).hover(function(){
        $(this).addClass("answer_hover_row");
        this.style.visibility = "visible";
    }, function(){
        $(this).removeClass("answer_hover_row");
        this.style.visibility = "hidden";
    });
}
$(".rowN").hover(function(){
    $(this).addClass("answer_hover_row");
    this.style.visibility = "visible";
}, function(){
    $(this).removeClass("answer_hover_row");
    this.style.visibility = "hidden";
});
或者在您的示例中添加类似的内容:

$('.entry_row').each(function() {
    var row = this;
    ...
});

您可以通过一次选择所有行来迭代行,但您需要向所有行添加一次额外的类,假设您添加了一个css类“hover rows”,请执行以下操作:

$('.hover rows')。每个(函数(){ $(this.mouseenter(function()){ $(this.addClass(“answer\u hover\u row”).css(“可见性”,“可见”); }) .mouseleave(函数(){ $(this).removeClass(“answer\u hover\u row”).css(“可见性”、“隐藏”); }); });
这就是你要找的吗

$('.hover-rows').each(function () { $(this).mouseenter(function(){ $(this).addClass("answer_hover_row").css('visibility', ''visible'); }) .mouseleave(function(){ $(this).removeClass("answer_hover_row").css('visibility', 'hidden'); }); });
var cnt=parseInt($('#counter').val(),10);

对于(var i=0;i如果有cnt数量的行,每个行都有class=“row1”、class=“row2”等,那么您可以使用这个jQuery(我切换到了更紧凑的.hover()函数),我假设行数来自计数器对象的innerHTML:

var cnt = parseInt($('#counter').val(),10);
for(var i=0;i<counter;i++){
    $(".row" + i).mouseenter(function(){
        $(this).addClass("answer_hover_row").show()
    }).mouseleave(function(){
        $(this).removeClass("answer_hover_row").hide()
    });
}

你的问题有点让人困惑。你是否使用id=“”和class=“”来处理同一个标记?你真的想在mouseOut上隐藏容器吗?无论如何,据我所知,你确实想在某个对象上切换悬停事件。这方面的纯jQuery解决方案如下所示:

 var cnt = document.getElementById('counter');
 //row1 is incremental and based on some value from 
 // database this value will increase. want to create a loop Here

$(".row1").mouseenter(function(){
    $(this).addClass("answer_hover_row");
    document.getElementById('row1').style.visibility = "visible";
}).mouseleave(function(){
    $(this).removeClass("answer_hover_row");
    document.getElementById('row1').style.visibility = "hidden";
});

$(".row2").mouseenter(function(){
    $(this).addClass("answer_hover_row");
    document.getElementById('row2').style.visibility = "visible";
}).mouseleave(function(){
    $(this).removeClass("answer_hover_row");
    document.getElementById('row2').style.visibility = "hidden";
});
var cnt = parseInt(document.getElementById('counter').innerHTML, 10);
for (var i = 1; i <= cnt; i++) {
    $(".row" + i).hover(function(){
        $(this).addClass("answer_hover_row");
        this.style.visibility = "visible";
    }, function(){
        $(this).removeClass("answer_hover_row");
        this.style.visibility = "hidden";
    });
}
$(".rowN").hover(function(){
    $(this).addClass("answer_hover_row");
    this.style.visibility = "visible";
}, function(){
    $(this).removeClass("answer_hover_row");
    this.style.visibility = "hidden";
});
我假设您可以使用非常短的代码,利用、和初始方法的组合。未经测试的建议:

$( 'DIV' ).hover(
    function () {  // MouseOver-Event
        $(this).addClass( 'answer_hover_row' );
        $( '#row1' ).show();
    },
    function () {  // MouseOut-Event
        $(this).removeClass( 'answer_hover_row' );
        $( '#row1' ).hide();
    }
);

你是在问如何从数据库检索数据,还是如何为
循环创建一个
?在这种情况下,你不应该真的使用任何类型的循环-选择器应该足够了。另外,为什么你要将jQuery选择器与普通的js DOM选择器混合/匹配?请发布一个HTML示例。最好的解决方案不太可能是在循环中添加事件处理程序。更好的方法是使用jQuery的live/delegate方法。如果您提供HTML,那么我可以发布一个示例作为答案,但目前还不够。感谢所有人的建议。我只是一名php开发人员,必须合并提供的设计。因此,我找到了最好的解决方案我是否将整个jquery包含在打印数据的循环中。
$(“.row”).mouseover(function(){//code
$( 'DIV' ).hover( function() { $(this).toggleClass( 'className' ).toggle(); } );