如何在javascript中查找li标记并替换为另一个li标记
我想找到具有特定id的li,并将其替换为ll标记 这是密码如何在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
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();
}
});