通过遵循HTML lang值更改内部HTML

时间:2014-02-18 12:11:34

标签: javascript jquery html

目前我正在使用多种语言的网站,我想在该网站上添加该语言栏。但我的问题是 - 它一直在框内说英文+英文图标,即使它是另一种语言,它应该是相应的图标+语言名称。

我只想按照html lang值更改innerhtml代码。我对JavaScript有基本的了解。

直播网站 - http://uposonghar.com/lang.html

我的代码 -

<nav role="custom-dropdown" id="nav_language" dir="ltr">
<input type="checkbox" id="button">
<label for="button" id="labelLang" onclick></label>
<ul>
    <li><a hreflang="en" href="http://chitrchatr.com/en/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/en_US.png" title="English" alt="English" />&nbsp;English</a></li>
    <li><a hreflang="de" href="http://chitrchatr.com/de/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/de_DE.png" title="Deutsch" alt="Deutsch" />&nbsp;Deutsch</a></li>
    <li><a hreflang="ru" href="http://chitrchatr.com/ru/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ru_RU.png" title="Русский" alt="Русский" />&nbsp;Русский</a></li>
    <li><a hreflang="es" href="http://chitrchatr.com/es/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/es_ES.png" title="Español" alt="Español" />&nbsp;Español</a></li>
    <li><a hreflang="zh" href="http://chitrchatr.com/zh/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/zh_CN.png" title="中文 (中国)" alt="中文 (中国)" />&nbsp;中文 (中国)</a></li>
    <li><a hreflang="ar" href="http://chitrchatr.com/ar/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ar.png" title="العربية" alt="العربية" />&nbsp;العربية</a></li>
    <li><a hreflang="fr" href="http://chitrchatr.com/fr/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/fr_FR.png" title="Français" alt="Français" />&nbsp;Français</a></li>
    <li><a hreflang="it" href="http://chitrchatr.com/it/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/it_IT.png" title="Italiano" alt="Italiano" />&nbsp;Italiano</a></li>
    <li><a hreflang="in" href="http://chitrchatr.com/in/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/id_ID.png" title="Bahasa Indonesia" alt="Bahasa Indonesia" />&nbsp;Bahasa Indonesia</a></li>
    <li><a hreflang="ko" href="http://chitrchatr.com/ko/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ko_KR.png" title="한국어" alt="한국어" />&nbsp;한국어</a></li>
    <li><a hreflang="th" href="http://chitrchatr.com/th/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/th.png" title="ไทย" alt="ไทย" />&nbsp;ไทย</a></li>
    <li><a hreflang="tl" href="http://chitrchatr.com/tl/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/tl.png" title="Tagalog" alt="Tagalog" />&nbsp;Tagalog</a></li>
    <li><a hreflang="hi" href="http://chitrchatr.com/hi/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/hi_IN.png" title="हिन्दी" alt="हिन्दी" />&nbsp;हिन्दी</a></li>
    <li><a hreflang="ja" href="http://chitrchatr.com/ja/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ja.png" title="日本語" alt="日本語" />&nbsp;日本語</a></li>
    <li><a hreflang="sv" href="http://chitrchatr.com/sv/"><img src="http://chitrchatr.com/wp-content/plugins/polylang/flags/sv_SE.png" title="Svenska" alt="Svenska" />&nbsp;Svenska</a></li>
</ul>
</nav>

使用Javascript -

<script>
if(document.getElementsByTagName('html').getAttribute('lang') = "en-US"){
    document.getElementById('labelLang').innerHTML = '<img alt="English" title="English" src="http://chitrchatr.com/wp-content/plugins/polylang/flags/en-US.png">&nbsp;English';
}
if(document.getElementsByTagName('html').getAttribute('lang') = "de-DE"){
    document.getElementById('labelLang').innerHTML = '<img alt="Deutsch" title="Deutsch" src="http://chitrchatr.com/wp-content/plugins/polylang/flags/de_DE.png">&nbsp;Deutsch';
}
if(document.getElementsByTagName('html').getAttribute('lang') = "ru-RU"){
    document.getElementById('labelLang').innerHTML = '<img alt="Русский" title="Русский" src="http://chitrchatr.com/wp-content/plugins/polylang/flags/ru_RU.png">&nbsp;Русский';
}
else{}
</script>

2 个答案:

答案 0 :(得分:2)

您未在<img>中关闭innerHTML的代码。

此外,

document.getElementsByTagName('html').getAttribute('lang') = "en-US" 是一项不是平等检查的任务。请改用==

正如@Akamaozu document.getElementsByTagName()指出的那样 返回HTMLCollection

答案 1 :(得分:2)

document.getElementsByTagName返回与标记匹配的元素数组,即使只有一个匹配项也是如此。

您需要做的是document.getElementsByTagName('html')[0]以获取该函数返回的第一个标记,该标记应该是您的HTML标记。

此外,如果您想进行比较,=====就是您要找的对象。 =只设置一个值。