<!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中代码为:
1. <span id="testid">aaaa</span>
获取的js代码:
copy
1. var x1 = document.getElementById("testid").innerText;
2. alert("x1="+x1);
copy
var x2 = document.getElementById("testid").innerHTML; alert("x2="+x2);