问题描述:

Could anyone help me how can i remove the content of a div tag with javascript?

I tried with this:

It removes the level1 div (and of course all div tag inside), but how can i remove only the content in level3 inside?

<script language=javascript>

function destroyDiv() {

var div = document.getElementById("level1");

div.parentNode.removeChild(div);

}

</script>

<div id="level1">

<div id="level2">

<div id="level3">

<label> Test content </label>

</div>

</div

</div>

<div id=blahDiv>Remove me</div>

<input type=button value='Remove the div' onclick='destroyDiv()'>

网友答案:

This sould work document.getElementById("level3").innerHTML = ''; but try thinking about using jQuery, because .innerHTML implementation differs in some browsers. jQuery would look like $('#level3').html('');

网友答案:

Why don't you access directly level3 ?

   document.getElementById("level3").innerHTML = "";
网友答案:

You can use:

document.getElementById("level3").innerHTML = "";
网友答案:

How about this?

var div = document.getElementById("level3");
div.innerHTML = "";
相关阅读:
Top