简体   繁体   中英

how to disable autoclosing of tags in jquery

I want to break my <ul> into multiple <ul> tags, I have 10 <li> in my <ul> and I want to break into 2 <ul> (5 <li> each). To do the same, I am taking 5th <li> and appending </ul><ul> using $(this).after().

But Jquery automatically reverse the text and auto close the <ul> .

Any Idea how to disable to auto close?

Cheers, Felix

While David's solution is an elegant one for the specific problem, another possibility for the general auto-closing tags problem would be to not use .append at all. Use a variable instead, and use .html(my_variable) at the end. I came across this while trying to do nested UL lists for this very simple Table Of Content thing:

$(document).ready(function() {
  var prev='';
  var toc = '';
  $("h2, h3, h4, h5").each(function(i) {
    var h = $(this);
    var level = h.attr("tagName");
    if (level != prev) {
      if (prev == '') {
        toc += "<ul>\n";
      }
      else {
        if (level < prev) {
          toc += "</ul>\n";
        }
        else {
          toc += "<ul>\n";
        }
      }
      prev = level;
    }
    toc += '<li><a href="#' + h.attr('id') + '">' + h.html() + "</a></li>\n";
  });
  toc += '</ul>';
  $("#toc").html(toc);
});

You do not want to disable auto close. What you want is to add a new UL after the first one, and then add to that one, the last five LI s of the first list. To get a reference to the LI s following the first five, you can do:

$('myFirstUL li:gt(4)');

gt being greater than zero-based index.

Full code example: http://jsbin.com/ofowe/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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