Javascript 绑定一个each语句

Javascript 绑定一个each语句,javascript,jquery,Javascript,Jquery,如果我想将每条语句绑定到#container div,我该怎么做 下面是我试图编写的每个语句的示例: $.each($(".product-comment"), function (key, value) { var showmoreHtml = $(this).html(); var showlessHtml = showmoreHtml.substr(0, 400); if (showmoreHtml.length > 400) { $(thi

如果我想将每条语句绑定到#container div,我该怎么做

下面是我试图编写的每个语句的示例:

$.each($(".product-comment"), function (key, value) {

    var showmoreHtml = $(this).html();
    var showlessHtml = showmoreHtml.substr(0, 400);
    if (showmoreHtml.length > 400) {
        $(this).html(showlessHtml).append("<a href='' class='product-comment-more'> (...Show More)</a>");
    } 
    $(this).on("click", ".product-comment-more", function (event) {
        event.preventDefault();
        $(this).parent(".product-comment").html(showmoreHtml).append("<a href='' class='product-comment-less'> (Show less)</a>");

});


$.ajax({
    method: 'POST',
    url: urlCreateReview,
    data: form_data,
    cache: false,
    processData: false,
    contentType: false,
    success: function(data) {
        $(".tab-comment-holder").load(" .tab-comment-holder");
    },
$。每个($(“.product comment”)、函数(键、值){
var showmoreHtml=$(this.html();
var showlessHtml=showmoreHtml.substr(0400);
如果(showmoreHtml.length>400){
$(this.html(showlessHtml).append(“”);
} 
$(此).on(“单击“,”.product comment more”),函数(事件){
event.preventDefault();
$(this).parent(“.product comment”).html(showmoreHtml).append(“”);
});
$.ajax({
方法:“POST”,
url:urlCreateReview,
数据:表格数据,
cache:false,
processData:false,
contentType:false,
成功:功能(数据){
$(.tab注释持有者”).load(“.tab注释持有者”);
},

您可以在选择器上调用jquery函数,调用
.each()
并传递
函数

$("#container div").each(function() {
    //do something
    console.log($(this));
});
编辑:

显然,
div
在AJAX回调中被“重新加载”。解决方案是确保其项在回调中正确初始化。为此,有一个.each(),但它使用如下命令

$(this).on("click", ".product-comment-more", function (event) {
    //...
}

这会为每个标签创建一个
点击处理程序,每次都有更多的产品注释类,这意味着如果有n个这样的标签,并且用户碰巧点击其中一个,点击处理程序将被执行n次。最好为每个标签创建一个处理程序。
showmoreHtml
没有很好的定义

当使用load函数重新加载div时,我没有到达单击位,因为showmorehtml不会将每个注释放入子字符串中,这可能是因为它没有绑定,因此没有显示更多或显示更少选项。这就是我试图做的,让显示更多和显示更少工作,以便我可以单击它们。如果您可以弥补您的不足,请如果您有自己的场景以及如何进行,我们将不胜感激。@detinu20请创建一个JSFIDLE。