简体   繁体   English

如何禁用jQuery中标签的自动关闭

[英]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). 我想打破我的<ul>成多个<ul>标记,我有10个<li>在我<ul>和欲打入2 <ul> (5 <li>每个)。 To do the same, I am taking 5th <li> and appending </ul><ul> using $(this).after(). 为此,我将使用第5个<li>并使用$(this).after()附加</ul><ul>

But Jquery automatically reverse the text and auto close the <ul> . 但是Jquery会自动反转文本并自动关闭<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. 尽管David的解决方案对于特定问题而言是一种优雅的解决方案,但一般自动关闭标签问题的另一种可能性是根本不使用.append Use a variable instead, and use .html(my_variable) at the end. 请改用变量,并在末尾使用.html(my_variable) I came across this while trying to do nested UL lists for this very simple Table Of Content thing: 我在尝试为这个非常简单的目录问题创建嵌套的UL列表时遇到了这个问题:

$(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. 您想要的是在第一个列表之后添加新的 UL ,然后将第一个列表的最后五个LI添加到该列表中。 To get a reference to the LI s following the first five, you can do: 要获得前五个之后的LI的引用,可以执行以下操作:

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

gt being greater than zero-based index. gt 大于从零开始的索引。

Full code example: http://jsbin.com/ofowe/edit 完整的代码示例: http : //jsbin.com/ofowe/edit

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

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