我试图找到ID为旧价格的div
并将其删除,然后再插入文档(main.appendChild(newDiv);
之前)。
p.s。:无法使用 JQuery
我已经尝试过了:
newDiv.getElementById(" old-price")。innerHTML ="";
但不起作用
任何帮助将不胜感激。
var divString = "<div class='col-xs-12 col-sm-6 col-md-4 col-lg-4'> <div class='thumbnail'> <h1 class='text-center'> Black Shoes with red stripes </h1> <img src='img/02.jpg'> <small id='old-price' class='old-price'>250</small> <p class='price'>250</p> <a class='btn btn-primary'>BUY</a> </div> </div> ";
var newDiv = document.createElement('div');
newDiv.innerHTML = divString;
//remove the div with id old-price
var main = document.getElementById("main");
main.appendChild(newDiv);
&#13;
<div id="main">
</div>
&#13;
答案 0 :(得分:4)
使用newDiv.querySelector("#old-price").remove()
。
var divString = "<div class='col-xs-12 col-sm-6 col-md-4 col-lg-4'> <div class='thumbnail'> <h1 class='text-center'> Black Shoes with red stripes </h1> <img src='img/02.jpg'> <small id='old-price' class='old-price'>250</small> <p class='price'>250</p> <a class='btn btn-primary'>BUY</a> </div> </div> ";
var newDiv = document.createElement('div');
newDiv.innerHTML = divString;
//remove the div with id old-price
newDiv.querySelector("#old-price").remove()
var main = document.getElementById("main");
main.appendChild(newDiv);
&#13;
<div id="main">
</div>
&#13;
答案 1 :(得分:1)
试试这个:
var divString = "<div class='col-xs-12 col-sm-6 col-md-4 col-lg-4'> <div class='thumbnail'> <h1 class='text-center'> Black Shoes with red stripes </h1> <img src='img/02.jpg'> <small id='old-price' class='old-price'>250</small> <p class='price'>250</p> <a class='btn btn-primary'>BUY</a> </div> </div> ";
var newDiv = document.createElement('div');
newDiv.innerHTML = divString;
//remove the old price
newDiv.getElementsByClassName("old-price")[0].innerHTML="";
var main = document.getElementById("main");
main.appendChild(newDiv);
答案 2 :(得分:0)
您可以尝试隐藏它:
document.getElementById("old-price").style.display = 'none';
或者将其作为子元素从父元素中删除(替换父ID):
document.getElementById("parentID").removeChild("old-price");