Saya menghasilkan <ul> dan sub <ul>, yang ingin saya lakukan adalah menempatkan struktur html daftar hanya dengan nilainya di dalamnya. Dengan kode berikut saya masuk ke area teks semua konten <ul> bukan hanya:

HTML

 <div id="mindMap">
   <ul class="list-unstyled margin-bottom-20">
      <li><button class="btn btn-default ul-appending margin-bottom-20">+ voce</button></li>
   </ul>
 </div> 

<div id="mindMapData">
  <textarea name="usp-custom-11" id="usp-custom-11" data-required="false" placeholder="Example Input 11" data-richtext="false" class="usp-input usp-textarea usp-form-365" rows="0" cols="30" style="margin: 0px; width: 871px; height: 291px;"></textarea>
</div>

jQuery

$('body').on('click', 'button.ul-appending', function() {
  $(this).parent().append(
    $('<ul class="main_ul list-unstyled margin-bottom-20">').addClass('newul').append(
       $('<li class="margin-bottom-20"><div class="input-group margin-bottom-20"><input placeholder="Aggiungi una voce..." class="form-control" type="text"><div class="input-group-btn"><button type="button" class="btn btn-default list">+ sotto voce</button><button type="button" class="btn btn-default removeThis">elimina</button></div></div></li>')
        )
    );
});

$('body').on('click', 'button.list', function() {
    var newLi = '<ul class="sub_ul list-unstyled margin-bottom-20"><li class="listSub margin-bottom-20"><div class="input-group margin-bottom-20"><input placeholder="Aggiungi una sotto voce..." class="form-control" type="text"><div class="input-group-btn"><button type="button" class="btn btn-default list">+ sotto voce</button><button type="button" class="btn btn-default removeThis">elimina</button></div></div></li></ul>';
    var listEl = $(this).parent().parent().parent();
    $(listEl).append(newLi);
});

Kemudian saya memeriksa perubahan html dan memasukkan html ke dalam textarea seperti ini:

$("#mindMap").on("DOMSubtreeModified",function(){
   $("#mindMapData textarea").val($("#mindMap").html());
});

Di area teks saya mendapatkan semua html:

<ul class="main_ul list-unstyled margin-bottom-20 newul">
  <li class="margin-bottom-20">
    <div class="input-group margin-bottom-20">
       <input placeholder="Aggiungi una voce..." class="form-control" type="text">
       <div class="input-group-btn">
         <button type="button" class="btn btn-default list">+ sotto voce</button>
         <button type="button" class="btn btn-default removeThis">elimina</button>
      </div>
   </div>
   <ul class="sub_ul list-unstyled margin-bottom-20">
     <li class="listSub margin-bottom-20">
       <div class="input-group margin-bottom-20">
         <input placeholder="Aggiungi una sotto voce..." class="form-control" type="text">
       <div class="input-group-btn">
         <button type="button" class="btn btn-default list">+ sotto voce</button>
         <button type="button" class="btn btn-default removeThis">elimina</button>
       </div>
     </div>
  </li>
</ul>

Ini dia jsFiddle yang berfungsi, lihat konten di textarea salah, saya mencoba mendapatkannya :

<ul>
  <li>Added node
    <ul>
      <li>Added sub node</li>
    </ul>
  </li>
 </ul>
1
rob.m 12 Maret 2017, 17:43

2 jawaban

Jawaban Terbaik

Anda dapat melintasi DOM dengan kode ini:

function ul(indent) {
  indent = indent || 4;
  var node = $(this);
    return node.removeAttr('class').children().map(function() {
    var self = $(this);
    var value = self.find('> .input-group input').val();
    var sub_ul = self.find('> ul');
    var ul_spaces = new Array(indent+4).join(' ');
    var li_spaces = new Array(indent).join(' ');
    if (sub_ul.length && ul) {
        return li_spaces + '<li>' + value + '\n' + ul_spaces +
          '<ul>\n' + ul.call(sub_ul, indent+8) + '\n' + ul_spaces + '<ul>\n' +
          li_spaces + '</li>';
    } else {
        return li_spaces + '<li>' + value + '</li>';
    }
  }).get().join('\n');
}

function updateTree() {
   $("#mindMapData textarea").val('<ul>\n' + $("#mindMap").clone().find('.main_ul').map(ul).get().join('\n') + '\n</ul>');
}

Satu sisi catatan Anda harus memanggil fungsi updateTree pada keyup untuk setiap input karena DOMSubtreeModified tidak diaktifkan ketika input mengubah nilainya, lihat pembaruan biola http://jsfiddle.net/44yb96Lb/72/

2
jcubic 12 Maret 2017, 15:43

Saya pikir menguraikan pohon DOM secara rekursif dan memfilter tag yang tidak diinginkan jauh lebih mudah dipahami. Lihat:

/**
 * @param {array<string>} allowedTags A list of tags that are allowed in the output
 * @returns {function} A function that takes a jQuery elements and returns a copy with only the allowed elements
 */
function filterElementsFactory(allowedTags) {
    allowedTags = allowedTags.map(function (tag) { return tag.toUpperCase(); });

    /**
     * @param {object} element jQuery element
     * @returns {documentFragment}
     */
    return function filterElements(element) {
        element = element.clone();
        var elementList = element.contents();
        var finalElem = document.createDocumentFragment();
        for (element of elementList) {
            if (element.nodeType === Node.TEXT_NODE) {
                finalElem.appendChild(element);
            } else if (element.nodeType === Node.ELEMENT_NODE) {
                if (allowedTags.indexOf(element.tagName) !== -1) {
                    var elemFrame = document.createElement(element.tagName);
                    elemFrame.appendChild(filterElements($(element)))
                    finalElem.appendChild(elemFrame);
                } else {
                    finalElem.appendChild(filterElements($(element)));
                }
            } 
        };
        return finalElem;
    }
}

Anda dapat menggunakan ini seperti ini:

var allowedTags = ['ul', 'li'];
var filterElements = filterElementsFactory(allowedTags);
$("#mindMap").on("DOMSubtreeModified",function(){
   var placeholderDiv = $('<div/>').append(filterElements($("#mindMap")));
   $("#mindMapData textarea").val(placeholderDiv.html());
});
1
squgeim 12 Maret 2017, 16:22