本文介绍了如何通过javascript获取div的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图获得名为'topage'的div的内容,我做了这段代码 < script type = 文本/ JavaScript的 > var topagge = document.getElementsByClassName(topage); console.log(topagge); < / script> 在控制台显示如下: HTMLCollection [1] 0:span.topage ... innerText:mycontentofdiv长度:1 原始 :HTMLCollection 我想获得innerText值,我试试这段代码: console.log(topagge.innerText); 但在控制台中它是未定义的 请问如何获得这个集合html对象的内容(使用attribut 0:span.topage这个包含innerText的内容。) 解决方案 您需要等到HTML docuemnt完全加载完毕后,试试这个: < script type =text / javascript> ; window.onload = function(){ var topagge = document.getElementsByClassName(topage); console.log(topagge); } 编辑: window.getElementsByClassName返回一个元素数组;然后,下面的代码将获得第一个元素的HTML内容: topagge [0] .innerHTML i'm trying to get content of div named 'topage' , i did this code<script type="text/javascript"> var topagge = document.getElementsByClassName("topage"); console.log(topagge); </script>in the console it shows like this :HTMLCollection[1] 0: span.topage ... innerText:"mycontentofdiv" length: 1 proto: HTMLCollectioni want to get the innerText value , i try this code : console.log(topagge.innerText);but in the console it's undefinedplease how can i get content of this collection html object ( using the attribut 0 : span.topage wich contain the innerText. 解决方案 You need to wait until the HTML docuemnt is fully loaded. Try this:<script type="text/javascript">window.onload = function(){ var topagge = document.getElementsByClassName("topage"); console.log(topagge);}</script>EDIT:window.getElementsByClassName returns an array of elements; then, following code will get HTML content of first element:topagge[0].innerHTML 这篇关于如何通过javascript获取div的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 19:16