简体   繁体   English

将LI元素转换为DIV

[英]Convert LI elements to DIV

I have a list of items inside a nested div with parent, grand parent and great grand parent div s. 我在嵌套div中有一个项目列表,其中有parent,grandparent和great grandparent div s。 The html code looks like this : html代码如下所示:

<div class="marketing">
  <div class="module-surround">
    <div class="module-content">
      <div class="k2ItemsBlock">
        <ul>
          <li><div>4</div></li>
          <li><div>5</div></li>
          <li><div>6</div></li>
          <li><div>7</div></li>
        </ul>
      </div>
    </div>
  </div>
</div>

I want to convert all the listed items to parent divs and use all the listed items to replace the great grand parent div, so that the overall code looks like this : 我想将所有列出的项目转换为父div,并使用所有列出的项目替换伟大的盛大父div,以便整体代码如下所示:

<div><div>4</div></div>
<div><div>5</div></div>
<div><div>6</div></div>
<div><div>7</div></div>

The on my logic, I have want to use jQuery to achieve this, and so far I am able to get all the elements into a listed item inside .K2ItemsBloc ul into a variable and replace div.marketing with the list, my jQuery code to do that looks like this 按照我的逻辑,我想使用jQuery来实现这一点,到目前为止,我已经能够将所有元素放入.K2ItemsBloc ul中的列表项中,并放入变量中,并将div.marketing替换为列表,我的jQuery代码看起来像这样吗

//1. Get all content in K2ItemsBlock ul into a variable called mlist
    var mlist = $(".k2ItemsBlock ul").contents()
//2. Change all li HTML elements inside the variable mlist to div

//3. Replace div.marketing with the variable mlist
    $(".marketing").replaceWith(mlist);

The above line of jQuery code without task #2 converts my original code to 上面没有任务#2的jQuery代码行将我的原始代码转换为

<li><div>4</div></li>
<li><div>5</div></li>
<li><div>6</div></li>
<li><div>7</div></li>

What I can figure out now is the syntax to convert all the li elements inside the variable mlist before replace div.marketing with it. 我现在能弄清楚的是在用div.marketing替换变量mlist之前转换所有li元素的语法。

Any suggestions or ideas? 有什么建议或想法吗?

Not sure if a div is a valid child of an ul element.. but you can do it like this 不知道div是否是ul元素的有效子元素。.但是您可以这样做

$('.k2ItemsBlock li>div').unwrap() // <--- removes the li
                      .wrap('<div/>'); // <-- adds the div

FIDDLE 小提琴

EDIT: I edited to add li>div just to make it more specific 编辑:我编辑添加li>div只是为了使其更具体

If you want only the top level li/divs to be affected.. change your selector to this 如果只希望影响顶层li / div,则将选择器更改为此

$('.k2ItemsBlock > ul > li > div') // <--  ">" is direct child selector

A full solution would be: 完整的解决方案是:

var marketing = $(".marketing");
var content = marketing.children().remove();
content.find(".k2ItemsBlock li>div").unwrap().wrap("<div>");
marketing.replaceWith(content.find(".k2ItemsBlock li>div");

This should grab all but the top level div. 除顶级div外,这应会抓住所有其他内容。 Remove it from the dom, perform manipulation within Javascript for efficiency. 从dom中删除它,在Javascript中执行操作以提高效率。 And then replace the top level div with the resulting html. 然后用生成的html替换顶级div。

mlist = mlist.replace('/li/g','div');

should do it. 应该这样做。 Might be better to replace <li> and </li> so you don't accidently replace the actual letters li if there are any. 最好替换<li></li>这样就不会意外替换实际的字母li(如果有的话)。

mlist = mlist.replace('/<li>/g','<div>').replace('/<\/li>/g','</div>');

You get the idea. 你明白了。

// Get an array of li elements
var mlist = $(".k2ItemsBlock ul li");
// new array of div's containing the content of li elements
var dlist = mlist.map(function(i, val){return "<div>" + $(val).html() + "</div>";});
// join div array into a single string
var result = dlist.toArray().join('');

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

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