[ js ] 怎麼辨識出HTML中的  

看板Ajax作者 (欲翔)時間15年前 (2009/07/15 14:14), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/2 (看更多)
我想用 js 辨識 HTML 中的一個文字節點,該節點含有&nbsp; 簡單的例子 <html> <span id="space">A&nbsp;B</span> </html> 我試過 javascript:alert('A B'==document.getElementById('space').firstChild.nodeValue) javascript: alert('A&nbsp;B'==document.getElementById('space').firstChild.nodeValue) javascript:alert('A%20B'==document.getElementById('space').firstChild.nodeValue) javascript: alert('A\u0020B'==document.getElementById('space').firstChild.nodeValue) 答案都是false,怎樣寫才會是true 環境:Fx3.5、xp sp3 謝謝。 -- 補充:如果該節點,沒有&nbsp; <html> <span id="space">AB</span> </html> javascript:alert('AB'==document.getElementById('space').firstChild.nodeValue) 會是true -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.175.196.140

07/15 16:08, , 1F
document.getElementById('space').innerHTML
07/15 16:08, 1F

07/15 18:18, , 2F
nodeValue辦不到嗎? //innerHTML不是標準
07/15 18:18, 2F

07/15 18:31, , 3F
這問題很有趣 不過我試不出來 &nbsp;→空白 似乎是單向的
07/15 18:31, 3F

07/15 18:31, , 4F
而且似乎只有瀏覽器能解讀
07/15 18:31, 4F

07/15 21:01, , 5F
不是標準的標準....你可以再試試看有沒有別的解法。
07/15 21:01, 5F
文章代碼(AID): #1ANNFNTF (Ajax)
文章代碼(AID): #1ANNFNTF (Ajax)