JS - 替换语言

时间:2013-07-14 12:37:22

标签: javascript

我的JS-File中有以下行:

   language : (div.lang && div.lang != "") ? div.lang : "en",

我只想更改以下行:

   language : (div.lang && div.lang != "") ? div.lang : "de",

   language : (div.lang && div.lang != "") ? div.lang : "es",

   language : (div.lang && div.lang != "") ? div.lang : "fr",

   language : (div.lang && div.lang != "") ? div.lang : "la",

   language : (div.lang && div.lang != "") ? div.lang : "it",

当我选择语言时,我想替换js文件中的行:

   <select id="type" name="type">
    <option value="en">English</option>
    <option value="de">German</option>
    <option value="es">Spanish</option>
    <option value="fr">French</option>
    <option value="la">Latin</option>
    <option value="it">Italian</option>
   </select>

但我不知道如何实现它:/有人可以帮忙吗?还有一个提示:我不想在前端更改访问者的语言。我只是想改变它以使用所选语言的整个脚本。

更新

我在JS-File中有以下内容:

   // Global Var in JS (placed on top)
   var LANGUAGE = "en";
   function languageChange(){ 
   LANGUAGE = document.getElementById("selectLanguage").value;
   }

我的HTML文件中有以下内容:

   <p style="float:left"><input style="border:1px solid #bbb" type="submit" value="Choose language" id="submit"></p>
   <p style="float:right; padding-right:20px">
   <select id="selectLanguage" onchange="languageChange">
   <option value="en">English</option>
   <option value="de">Deutsch</option>
   <option value="es">Español</option>
   <option value="fr">Français</option>
   <option value="la">Lingua latina</option>
   <option value="it">Italiano</option>
   </select>&nbsp;<input type="submit" value="Enter" id="submit"></p>

它不起作用。

2 个答案:

答案 0 :(得分:1)

//GLOBAL
var LANGUAGE = "en"; // if english is default


//on bodyload
$("#type").change(function(){
  LANGUAGE = $(this).val();
});


//wherever you want to use it
language : (div.lang && div.lang != "") ? div.lang : LANGUAGE,

评论:

  1. enum只是在浪费资源(在我看来==&gt; btw谢谢;))
  2. 使用我的代码基于jquery
  3. on bodyload可以在domready,html on bodyload等上放入jquery
  4. 对于纯JS:

    // HTML:
    
    <select id="selectLanguage" onchange="languageChange">
    
    // JS
    //// Global Var in JS (placed on top)
    var LANGUAGE = "en";
    
    //// function placed anywhere
    function languageChange(){
        LANGUAGE = document.getElementById("selectLanguage").value;
    }
    
    //// wherever you want to use the line
    language : (div.lang && div.lang != "") ? div.lang : LANGUAGE
    

    顺便说一句:     // onload事件     //// Html方式     

    //or
    //// JQ way
    $(document).ready(function{})
    
    // etc...
    

答案 1 :(得分:1)

var languageEnum = {
  english: "en",
  german: "de",
  spanish: "es",
  french: "fr",
  latin: "la",
  italian: "it"
};

var pageConfiguration = {
  language: languageEnum.english; // note that this could be a user preference too and you could check the browser's language
};

$(document).ready(function() {
  $("#type").change(function(){
    pageConfiguration.language = $(this).val();
  });
});

  //...
  language : (div.lang && div.lang != "") ? div.lang : pageConfiguration.language,
  //...