简体   繁体   English

使用jQuery附加到div

[英]attaching to a div with jquery

Pretty new to Jquery/Ajax/Js. Jquery / Ajax / Js的新手。 How would i go about binding/delegating the following code to either a container div or the document itself so that when i reload a div(that uses the code) with jquery the code does not break like it currently does in a document.ready(); 我将如何将以下代码绑定/委托到容器div或文档本身,以便当我通过jquery重新加载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>");
    } else {
        $(this).html(showmoreHtml);
    }
    $(this).on("click", ".product-comment-more", function (event) {
        event.preventDefault();
        $(this).parent(".product-comment").html(showmoreHtml).append("<a href='' class='sight-comment-less'> (Show less)</a>");
    });
    $(this).on("click", ".product-comment-less", function (event) {
        event.preventDefault();
        $(this).parent(".product-comment").html(showmoreHtml.substr(0, 400)).append("<a href='' class='product-comment-more'> (...Show More)</a>")
    });
}); 

First thing is to place that more/less function to add link in a named function... So you can run it from anywhere in the code. 首先是将更多/更少的函数添加到命名函数中,以添加链接...因此您可以在代码中的任何位置运行它。 Have all the logic there. 拥有所有逻辑。

Then, the click handlers (on more and less ) can be defined out of that loop. 然后,点击处理程序(关于更多更小 ),可以定义出该循环。 There is no use to define them more than once, since they are delegated. 定义它们是没有用的,因为它们被委托了。 And make it run the loop in the named function passing a true/false argument to the function. 并使其在命名函数中运行循环,并向该函数传递true / false参数。

Finally... On ajaxComplete , run that function again. 最后...在ajaxComplete上 ,再次运行该函数。

So all more/less fixes will be done on load, click and ajaxComplete. 因此,所有更多/更少的修复将在加载,单击和ajaxComplete上完成。

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

      // Remove any more/less link (if any)
      $(this).find("[class^='product-comment-']").remove();

      // Get the remaining HTML
      var HTML = $(this).html();

      // Decide about the more or less link. If "more" is false,
      if (HTML.length > 400 && more) {
        $(this).html(HTML).append("<a href='' class='product-comment-more'> (Show less)</a>");
      }
      if (HTML.length > 400 && !more) {
        $(this).html(HTML.substr(0, 400)).append("<a href='' class='product-comment-more'> (...Show More)</a>");
      }
      if (HTML.length <= 400) {
        $(this).html(HTML);
      }
  });
}

// On page load execution
moreLess(false);

// On ajax complete execution
$( document ).ajaxComplete(function(){
  moreLess(false);
});

// Click handler for more.
$(".product-comment").on("click", ".product-comment-more", function (event) {
  event.preventDefault();
  moreLess(true)
});

// Click handler for less.
$(".product-comment").on("click", ".product-comment-less", function (event) {
  event.preventDefault();
  moreLess(false)
});

Disclaimer: written quickly and untested... But I'm confidant it will work fine. 免责声明:写得很快,未经测试...但是我很自信,它可以正常工作。 Feel free to ask questions. 随意问的问题。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM