javascript实现unicode和字符的互相转换_javascript技巧

WBOY
リリース: 2016-05-16 19:11:20
オリジナル
839 人が閲覧しました

<script> <BR>//unicode转换为字符 <BR>function unicode2Chr(str) { <BR> if ('' != str) { <BR> var st, t, i <BR> st = ''; <BR> for (i = 1; i <= str.length/4; i ++){ <BR> t = str.slice(4*i-4, 4*i-2); <BR> t = str.slice(4*i-2, 4*i).concat(t); <BR> st = st.concat('%u').concat(t); <BR> } <BR> st = unescape(st); <BR> return(st); <BR> } <BR> else <BR> return(''); <BR>} <BR>//字符转换为unicode <BR>function chr2Unicode(str) { <BR> if ('' != str) { <BR> var st, t, i; <BR> st = ''; <BR> for (i = 1; i <= str.length; i ++){ <BR> t = str.charCodeAt(i - 1).toString(16); <BR> if (t.length < 4) <BR> while(t.length <4) <BR> t = '0'.concat(t); <BR> t = t.slice(2, 4).concat(t.slice(0, 2)) <BR> st = st.concat(t); <BR> } <BR> return(st.toUpperCase()); <BR> } <BR> else { <BR> return(''); <BR> } <BR>} <br><br>var http_request = false; <BR>function getRequest(url) { <BR> http_request = false; <BR> if (window.XMLHttpRequest) { // Mozilla, Safari,... <BR> http_request = new XMLHttpRequest(); <BR> if (http_request.overrideMimeType) { <BR> http_request.overrideMimeType('text/xml'); <BR> } <BR> } else if (window.ActiveXObject) { // IE <BR> try { <BR> http_request = new ActiveXObject("Msxml2.XMLHTTP"); <BR> } catch (e) { <BR> try { <BR> http_request = new ActiveXObject("Microsoft.XMLHTTP"); <BR> } catch (e) {} <BR> } <BR> } <BR> if (!http_request) { <BR> return false; <BR> } <BR> http_request.onreadystatechange = showtlinfo; <BR> http_request.open('GET', url, true); <BR> http_request.send(null); <BR>} <br><br>function showtlinfo() { <BR> if (http_request.readyState == 4) { <BR> if (http_request.status == 200) { <BR> txt=http_request.responseText; <BR> document.noname=txt; <BR> document.all.noname.innerHTML=txt; <BR> } else { <BR> return; <BR> } <BR> } <BR>} <BR>function tlinfo() <BR>{ <BR> var xml=new ActiveXObject("Microsoft.XMLDOM"); <BR> xml.async="false"; <BR> xml.load("http://my.x-push.net/xml/805128.xml"); <BR> if(xml.parseError!=0) <BR> { <BR> getRequest("http://my.x-push.net/getxml.asp?no=805128"); <BR> return; <BR> } <BR> var root=xml.documentElement; <BR> txt=root.childNodes(1).text; <BR> document.noname=txt; <BR> document.all.noname.innerHTML=txt; <BR>} <BR>tlinfo(); <BR></script>
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!