Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中查找li标记并替换为另一个li标记_Javascript_Jquery - Fatal编程技术网

如何在javascript中查找li标记并替换为另一个li标记

如何在javascript中查找li标记并替换为另一个li标记,javascript,jquery,Javascript,Jquery,我想找到具有特定id的li,并将其替换为ll标记 这是密码 var t = document.getElementById('idname').getAttribute('atrname'); $('li.classname').each(function(element) { if(t == $(this).text()){ $('.classname li').replaceWith('li'); } }); html

我想找到具有特定id的li,并将其替换为ll标记 这是密码

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

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


谢谢。

如果我正确理解了你的问题,这应该能回答你的问题:

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();
  }
});
var t=document.getElementById('idname').getAttribute('atrname');
$('li.classname')。每个(函数(元素){
如果(t=$(this).text()){
//id与原始LI相同的新LI元素
让newLi=“
  • 这是新的li,用#someId
  • ”替换上一个li; //在原始LI旁边追加新LI,然后删除原始LI $('#someId')。在(newLi.remove()之后; } });
    我也创建了一个。
    让我知道这是否有帮助

    同时添加您的HTMLL您所说的“ll”是什么意思,您想作为类或类替换element@Vishnudev替换元素而不是类(它的li标记)。要创建一个名为ll的自定义元素吗?@Vishnudev no want用另一个
  • 标记替换
  • 标记
    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();
      }
    });