2015-09-21 32 views
0

我寫一個可變進我的HTML元素作爲這樣的:獲取Unicode字符串的解碼版本?

document.getElementById('myDiv').innerHTML = '<xsl:value-of select="@value"/>'; 

但是它寫成這樣:

TECHNO A.&#x15E;. 

這應該是:

TECHNO A.Ş. 

我怎麼能手動獲取解碼版本的Unicode字符串與Javascript?

PS:我意識到這個問題發生在Chrome瀏覽器,而不是Internet Explorer。

+0

請向我們展示XML輸入和XML中的'value'屬性值輸入。 –

+0

來自服務器的響應是:'TECHNO A. & amp;#x15E; .' – kamaci

回答

0

&#and;是用於十六進制字符的html解析器轉義符。因此,您需要做的是剝離它們並將剩餘的'15E'值傳遞給此(例如):

<!DOCTYPE html> 
<div id="myId"> 
yo oyyoyoyooy &amp;amp;amp;#xAA;suuupspuspupsu 
    ssdosudoisduoisudoiud 
    &amp;amp;#xFE; 
</div> 


<script> 
document.addEventListener("DOMContentLoaded", function(){ 
    var nodeIterator = document.createNodeIterator(
    // Node to use as root 
    document.getElementById('myId'), 

    // Only consider nodes that are text nodes (nodeType 3) 
    NodeFilter.SHOW_TEXT, 

    // Object containing the function to use for the acceptNode method 
    // of the NodeFilter 
     { acceptNode: function(node) { 
     // Logic to determine whether to accept, reject or skip node 
     // In this case, only accept nodes that have content 
     // other than whitespace 
     if (/&.*?#x[^;]+;/ig.test(node.data)) { 
      return NodeFilter.FILTER_ACCEPT; 
     } 
     } 
    }, 
    false 
); 

    // Show the content of every non-empty text node that is a child of root 
    var node; 

    while ((node = nodeIterator.nextNode())) { 
    node.nodeValue = node.nodeValue.replace(/&.*?#x([^;]+);/ig, function(match, p1) { 
     return String.fromCharCode(parseInt(p1, 16)) 
    }); 
    } 
}); 
</script> 
+0

我不知道服務器會發生什麼。任何值都可以返回。 – kamaci

+0

@kamaci我更新了答案檢查出來,通常它會收集所有文本節點下的特定模式&...#x ....;並捕捉#x和之間的任何內容;並替換它們,這將會覆蓋您的所有案例,因爲所有來自服務器的數據都以相似的方式轉義。希望能幫助到你。 –