Wrap a span around some text in jQuery

后端 未结 3 820
孤城傲影
孤城傲影 2021-01-18 18:45

What I have :

  • My text
    • blahblahblah
3条回答
  •  情书的邮戳
    2021-01-18 19:33

    var ul = document.getElementById("myId");
    var li = ul.firstElementChild;
    var text = li.firstChild;
    var ul = li.childNodes[1];
    ul.classList.add('myClass');
    var span = document.createElement("span");
    span.textContent = text.data;
    li.replaceChild(span, text);
    

    Old fashioned DOM to the rescue.

提交回复
热议问题