簡體   English   中英

如何在javascript中找到li標簽並替換為另一個li標簽

[英]How to find li tag and replace with another li tag in javascript

我想找到具有特定 id 的 li 並將其替換為 ll 標簽這是代碼..

    var t = document.getElementById('idname').getAttribute('atrname');
     $('li.classname').each(function(element) {

       if(t == $(this).text()){
       $('.classname li').replaceWith('li');
     }
   });

html

<ul class="classname">
   <li id="idname" attrname"name"></li>
</ul>

謝謝..

如果我正確理解了您的問題,這應該可以回答您的問題:

var t = document.getElementById('idname').getAttribute('atrname');
$('li.classname').each(function(element) {
  if(t == $(this).text()){
      // New LI element with same id as original LI
      let newLi = "<li id='someId'>This is new li replacing prev LI with #someId</li>";
      // Appending new LI next to original LI and then removing original LI
      $('#someId').after(newLi).remove();
  }
});

我也創建了一個jsFiddle 讓我知道這是否有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM