![](/img/trans.png)
[英]javascript (document.getelementbyid) in an echo not working (probably wrong usage of quotes)
[英]Handling double quotes in document.getElementById
我試圖從隱藏的html輸入中獲取JSON字符串。 JSON字串:
"{
\"title\": \"Air Quality Details\",
\"type\": \"object\",
\"properties\": {
\"lead\": {
\"type\": \"string\",
\"title\": \"Lead\"
},
\"nitrogen\": {
\"type\": \"string\",
\"title\": \"Nitrogen\"
}
}
}"
<input type="hidden" id="uischema" value="{ \\" title\\":="" \\"air="" quality="" details\\",="" \\"type\\":="" \\"object\\",="" \\"properties\\":="" {="" \\"lead\\":="" \\"string\\",="" \\"title\\":="" \\"lead\\"="" },="" \\"nitrogen\\":="" \\"nitrogen\\"="" }="" }"="">
當我對此執行document.getElementById時,僅獲得“ {\\”而不是完整的字符串。
聽起來您正在這樣做:
<input type="hidden" id="something" value="{\"title\":...
在HTML中, \\
並非轉義字符,因此first "
結束value
屬性,就好像您編寫了value="{\\"
。
要在雙引號屬性中正確包含JSON,請使用"
:
<input type="hidden" id="something" value="{"title":...
但是 ,將JSON放在隱藏的輸入中可能不是一個好主意。 如果只想使數據在頁面上可用,請將其輸出到script
標簽或類似標簽。
您的示例中的字符串不能包含這樣的換行符。 相反,您應該用\\n
替換換行符,然后它應該可以正常工作。
或者,如果您能夠使用ES6功能,則可以使用use反引號將您的字符串括起來,然后您將可以在代碼中使用多行字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.