<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
function loadText(){
var spanValue = document.getElementById("getSpanText").innerHTML;
alert("spanValue = " + spanValue);
}
</script>
</head>
<body onload="loadText()">
<span id="getSpanText">Jason</span>
</body>
</html>
HTML中代码为:
- <span id="testid">aaaa</span>
获取的js代码:
方法一:.innerText
- var x1 = document.getElementById("testid").innerText;
- alert("x1="+x1);
方法二:.innerHTML
- var x2 = document.getElementById("testid").innerHTML;
- alert("x2="+x2);