简体   繁体   English

jQuery replaceWith不起作用</div>

[英]jquery replaceWith not working with </div>

I've been trying to figure this out for several hours without much luck.. I'll ask my specific question first, but then give you the bigger picture after in case my entire approach is wrong. 我一直试图弄清楚这个问题已经好几个小时了。我会先问我一个具体的问题,但是如果我的整体方法是错误的,然后再给您一个更大的认识。

Specific Question: I'm trying to replace a list item element with some closing tags. 特定问题:我正在尝试将列表项元素替换为一些结束标记。 The first IF statement below works, the second works with many values in the "html" variable, but as soon as I stick a closing div tag into the variable, the replace fails to occur. 下面的第一个IF语句可以工作,第二个IF语句可以在“ html”变量中使用许多值,但是一旦我将div结束标记插入变量中,替换就不会发生。 I can't figure it out. 我不知道。 I've tried escaping the slash in the closing tags with 1-2 backslashes and that doesn't help (with two backslashes the replace occurs but text is rendered on the page instead of code). 我尝试用1-2个反斜杠在结束标记中转义斜杠,但这无济于事(如果使用两个反斜杠,则会发生替换,但文本将呈现在页面上而不是代码中)。

$("li.class1").each(function() {
    var li = $(this);
    var span = li.find("span.class2").text();
    if (span.indexOf("<") == 0) {
        var text = $(this).text();
        text = text.substring(1, text.length);
        li.replaceWith('<li><div><ul><li class="Mheader">' + text + '</li>');
    }
    else if (span.indexOf(">") == 0) {
        var html = '</ul></div></li>';
        li.replaceWith(html);
        alert(html);
    }
});

Now, with that said.. I have noticed that FireBug shows closing tags after the first replace happens even though I haven't inserted them yet. 现在,这样说..我注意到FireBug在第一次替换发生后显示关闭标签,即使我还没有插入它们。 I'm hoping that this is just FireBug being smart and inserting them because they are missing. 我希望这只是FireBug的明智之举,并因为它们丢失而将它们插入。

What I'm actually trying to accomplish: We have an application that renders some plain flyout navigation menus as a large, vertical, unordered list. 我实际上要完成的工作:我们有一个应用程序,将一些普通的弹出导航菜单呈现为一个大型,垂直,无序的列表。 I want to insert some specially-formatted group names into this list (like below) and then use jquery to replace those specially-formatted group names to effectively chop these "groups" into DIVs. 我想在此列表中插入一些特殊格式的组名(如下所示),然后使用jquery替换那些特殊格式的组名,以有效地将这些“组”砍成DIV。 I will then use CSS to lay out each group horizontally in order to create some basic mega menu functionality. 然后,我将使用CSS水平排列每个组,以创建一些基本的大型菜单功能。 The example below correlates with my source code above. 下面的示例与上面的源代码相关。

  • < Header 1 <标头1
  • item 1 项目1
  • item 2 项目2
  • > >
  • < Header 2 <标头2
  • item 1 项目1
  • item 2 项目2
  • > >

Desired markup after jquery tweak: jQuery调整后所需的标记:

<ul>
<li>
    <div>
    <ul>
    <li class=header>header1</li>
    <li>item1</li>
    .....
    </ul></div>
</li>

Thanks for any feedback. 感谢您的任何反馈。 This is my go-to site when I need information like this, but this is the first time I've had to ask! 当我需要诸如此类的信息时,这是我的首选站点,但这是我第一次必须询问!

Thanks, 谢谢,

Tommy 汤米

The closing tags get automatically inserted. 结束标记将自动插入。

You could somehow use .wrapInner, or do something like this: 您可以以某种方式使用.wrapInner,或执行以下操作:

html = '';
$("li").each(function() { // I removed the class as I don't know if each li has one or if only headers, etc.
    var li = $(this);
    var span = li.text(); // Same here.. removed the class
    if (span.indexOf("<") == 0) {
        var text = $(this).text();
        text = text.substring(1, text.length);
        html += '<li><div><ul><li class="Mheader">' + text + '</li>';
    }
    else if (span.indexOf(">") == 0) {
        html += '</ul></div></li>';
    } else {
        html += '<li>' + li.html() + '</li>';
    }
});
$('ul').html(html);

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

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