简体   繁体   中英

how to get element id of div and appendChild html to it

I want to get the ID of a div using JavaScript that runs inside that div . I want to appendChild() html to that div .

eg:

<div id="randomnumber">
<script type="text/javascript"> 
var htmlcontent = "abcdf";
//need the id of div holder
var thisDIVid= ?????;
// appendChild to this div
?????.appendChild(htmlcontent);
</script>
</div>

document.getElementyById("yourDivId"); will find any div with an unique ID.

var div = document.getElementyById("yourDivId");
div.appendChild("yourContent");

If you know where in the structure your div is placed you can access it like this:

var mainDiv = document.getElementById('mainDiv');
yourDiv = mainDiv.getElementsByTagName('div')[number];

number is the place in the structure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM