How to remove li from ul within a div
I have a div, where in i do have a ul, where the ul has some li.
i just want to find some text and remove the li from th开发者_如何学JAVAe ul.
suppose eg:
<div id="div1">
<ul>
<li>Hello</li> <!--i need to remove this.-->
</ul>
</div>
var div = document.getElementById('div1'),
ul = div.getElementsByTagName('ul')[0],
li = ul.getElementsByTagName('li'),
len = li.length;
// Go backwards so that removing items has no effect
while (len--) {
if ( /Hello/.test(getText(li[len])) ) {
ul.removeChild(li[len]);
}
}
function getText(node) {
var s = '';
node = node.firstChild;
if ( node ) do {
if ( node.nodeType === 3 ) {
s += node.data;
}
if ( node.nodeType === 1 ) {
s += getText(node);
}
} while ( node = node.nextSibling );
return s;
}
With jQuery, you can do this:
$("#div1 ul li:contains('Hello')").remove();
function removeLi()
{
var DIV = document.getElementById('div1');
var LI = DIV.getElementsByTagName('li');
LI[0].parentNode.removeChild(LI[0]); //remove the first li
//LI[1].parentNode.removeChild(LI[1]); //remove the second li
}
精彩评论