简体   繁体   English

如何在单击按钮时从ul中解包所有li

[英]How to unwrap all li from ul on button click

This is probably a dummy question. 这可能是一个虚拟的问题。 I am trying to export the contents of a unordered list into a text area and remove the list item on export. 我正在尝试将无序列表的内容导出到文本区域,并在导出时删除列表项。 I have achieved it at some degree but it only brings in the first list item. 我在某种程度上已经实现了它,但是它仅带来了第一个列表项。 How can I export all the li's? 我怎么能出口所有的李氏?

HTML : HTML:

 <div class="wrapper">
    <button id="exportBtn">Export</button>
    <div class="container">
        <ul class="structure">
            <li>Something</li>
            <li>Something else</li>
        </ul>
    </div>
    <div class="text">
        <textarea id="exportTxt" contenteditable style=""></textarea> 
    </div>
</div>

This is my script : 这是我的脚本:

$(function() {
  $( "#exportBtn" ).click(function() {
    var htmlString = $('.structure li').html();

    $('textarea#exportTxt').html(htmlString).parent().is( 'li' ).htmlString.unwrap();
    $('textarea#exportTxt').addClass('slideIn');
  });
});

View working demo 查看工作演示

So you want it to read SomethingSomething else . 因此,您希望它阅读SomethingSomething else

$(function() {
  $( "#exportBtn" ).click(function() {
      var html = ''; // start with empty
      $('.structure li').each(function(){
          html += $(this).html(); // append the html from each li
          $(this).remove(); // remove the li
      });
      $('#exportTxt').val(html); // set it in the textarea
  });
});

Try this: 尝试这个:

js js

$("#exportBtn").click(function() {
    var list="";
    $(".structure").children().each(function(){
        list +=$(this).html() + " ";
    });
    $('textarea#exportTxt').val(list);
});

fiddle 小提琴

Here is a jsfiddle with a working demo: http://jsfiddle.net/Grimbode/PucV2/ Hope this is what you wanted. 这是一个带有正在运行的演示的jsfiddle: http : //jsfiddle.net/Grimbode/PucV2/希望这是您想要的。

js: js:

  $( "#exportBtn" ).on('click', function() {
      $('.structure li').each(function(){
          var txt = $('#exportTxt').text();
          $('#exportTxt').empty();
          $('#exportTxt').text(txt + ' ' + $(this).text());
          $(this).remove();
      });
  });

html: 的HTML:

<ul class="structure">
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>
<button id="exportBtn">Export</button>
<textarea id="exportTxt"></textarea>

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

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