如何獲取嵌入到腳本標記中的JSON數據?從<script>讀取JSON標記
<!DOCTYPE html>
<html>
<head>
<script id="data" type="application/json">{org: 10, items:['one','two']}</script>
<script type="text/javascript">
var obj = JSON.parse(document.getElementById('data').value);
console.log(obj);
console.log(obj.org);
</script>
</head>
<body>
</body>
</html>
我越來越:
未捕獲的SyntaxError:意外的標記ü在JSON在位置0
爲什麼使用則firstChild當腳本標籤沒有子元素,不僅僅是數據? –
@GTSJoe - 訪問元素節點內的文本節點。 – Quentin
僅僅使用document.getElementById('data').text是否有優勢? –