- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我通常使用 JavaScript 来实现 UI/交互目的,并且我正在尝试通过编写更复杂的 JS 来提高我的技能。
我编写了以下代码,试图根据 URL 和国家/地区代码更改我们网站的国家/地区选择器中的内容。
我知道这很残酷,但我花了几个小时,但我被困住了。如果有人可以提供可以帮助我前进的信息,我将非常感激。
我的所有变量都未使用,那是因为我认为我可以使用点符号在 if 语句中调用它们,但事实并非如此。
这是我到目前为止所写的内容。尽量不要笑(或哭)。谢谢你。
//Initialize Variables
var moduleTitle = document.querySelector(".amt-select-title").innerHTML;
var colTitle = document.querySelector(".amt-choose-country-col > h3").innerHTML;
var languageModTitle = document.querySelector(".amt-choose-language > h2").innerHTML;
var investorRelations = document.querySelector(".amt-utility-3 a").innerHTML;
var company = document.querySelector(".amt-utility-4 a").innerHTML;
var corporateResponsibility = document.querySelector(".amt-utility-5 a").innerHTML;
var careers = document.querySelector(".amt-utility-6 a").innerHTML;
var contactUs = document.querySelector(".amt-utility-7 a").innerHTML;
// This is my library of objects -- one for each language -- EN, ES, PT and DE
var ENcontent = {
moduleTitle : "English Module Title",
colTitle : "English Title",
languageModTitle : "Choose Language EN Title",
investorRelations : "Investor Relations",
company : "Company",
corporateResponsibility : "Corporate Responsbility",
careers : "Careers",
contactUs : "Contact Us"
};
var EScontent = {
moduleTitle : "Spanish Module Title",
colTitle : "Spanish Title",
languageModTitle : "Choose Language ES Title",
investorRelations : "Relaciones con Inversionistas",
company : "Empresa",
corporateResponsibility : "Responsabilidad Corporativa",
careers : "Oportunidades Laborales",
contactUs : "Contáctanos"
};
var PTcontent = {
moduleTitle : "Portuguese Module Title",
colTitle : "Portuguese Title",
languageModTitle : "Choose Language PT Title",
investorRelations : "Investidores",
company : "Empresa",
corporateResponsibility : "Responsabilidade Corporativa",
careers : "Carreiras",
contactUs : "Fale Conosco"
};
var DEcontent = {
moduleTitle : "German Module Title",
colTitle : "German Title",
languageModTitle : "Choose Language DE Title",
investorRelations : "Investor Relations",
company : "Unternehmen",
corporateResponsibility : "Corporate Responsbility",
careers : "Karriere",
contactUs : "Kontakt"
};
//Language Object
function Language(moduleTitle, colTitle, languageModTitle, investorRelations, company, corporateResponsibility, careers, contactUs) {
this.moduleTitle = moduleTitle,
this.colTitle = colTitle,
this.languageModTitle = languageModTitle,
this.investorRelations = investorRelations,
this.company = company,
this.corporateResponsibility = corporateResponsibility,
this.careers = careers,
this.contactUs = contactUs;
}
//Calling Language() function using arguments based on country -- What's a good way to do this?
if(window.location.href.indexOf("/en") > -1) {
Language(
ENcontent.moduleTitle,
ENcontent.colTitle,
ENcontent.languageModTitle,
ENcontent.investorRelations,
ENcontent.company,
ENcontent.corporateResponsibility,
ENcontent.careers,
ENcontent.contactUs);
}else if(window.location.href.indexOf("/es") > -1) {
Language(
EScontent.moduleTitle,
EScontent.colTitle,
EScontent.languageModTitle,
EScontent.investorRelations,
EScontent.company,
EScontent.corporateResponsibility,
EScontent.careers, EScontent.contactUs);
}else if(window.location.href.indexOf("/pt") > -1) {
Language(
PTcontent.moduleTitle,
PTcontent.colTitle,
PTcontent.languageModTitle,
PTcontent.investorRelations,
PTcontent.company,
PTcontent.corporateResponsibility,
PTcontent.careers,
PTcontent.contactUs);
}else if(window.location.href.indexOf("/de") > -1) {
Language(
DEcontent.moduleTitle,
DEcontent.colTitle,
DEcontent.languageModTitle,
DEcontent.investorRelations,
DEcontent.company,
DEcontent.corporateResponsibility,
DEcontent.careers,
DEcontent.contactUs);
}
最佳答案
首先,我将为您提供一个快速解决方案。像这样编写您的 Language
函数:
function Language(moduleTitle, colTitle, languageModTitle, investorRelations, company, corporateResponsibility, careers, contactUs) {
document.querySelector(".amt-select-title").innerHTML = moduleTitle,
document.querySelector(".amt-choose-country-col > h3").innerHTML = colTitle,
document.querySelector(".amt-choose-language > h2").innerHTML = languageModTitle,
document.querySelector(".amt-utility-3 a").innerHTML = investorRelations,
document.querySelector(".amt-utility-4 a").innerHTML = company,
document.querySelector(".amt-utility-5 a").innerHTML = corporateResponsibility,
document.querySelector(".amt-utility-6 a").innerHTML = careers,
document.querySelector(".amt-utility-7 a").innerHTML = contactUs;
}
这应该相应地更新页面。但是,您可能想研究 JS 中的对象引用(原始与对象)。特别是在函数内部使用关键字 this
和 window
属性(全局对象)时。例如:
var obj = {a: "Hello"};
var str = obj.a; // str === obj.a returns true
str = "World" // str === obj.a returns false
这是因为 obj.a
未链接到 str
。我们只是传递实际值。
但是,请注意这有何不同:
var obj1 = {a: "Hello"};
var obj2 = obj1; // obj2.a === obj1.a returns true
obj2.a = "World" // obj2.a === obj1.a STILL returns true
当您将对象分配给变量时,您正在传递一个引用。当您将基元分配给变量时,您正在传递它的值!
关于javascript - 根据国家/地区语言填充innerHTML 的JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160147/
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
在我的 MVC 应用程序中,我想显示用户访问网站的国家/地区、地区和城市。我怎样才能获取它们? 最佳答案 有几种方法。一种是要求用户与远程服务器共享他的位置。 https://developer.mo
有人知道Firebase电话身份验证是否适用于印度电话号码吗? 我成功实现并使用了美国号码(+1xxxxxxxxxx),但是尝试使用印度电话号码时未收到文本。不确定是否无法正常工作,或者我丢失了某些东
我想知道网站的原始国家/地区。我注意到Alexa网站通常可以检测网站的原始国家/地区。例如,Alexa可以检测到stackoverflow.com网站的原始国家/地区是美国,依此类推。 Alexa如何
我有一些文本,其中可能包含也可能不包含国家/地区名称。例如: ' Nigeria: Hotspot Network LTD Rural Telephony Feasibility Study' 这就是
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我有一组值,它们是我的 ajax 调用中的国家/地区代码 Handler.getCountryIdparam1, {callback:function(data){ // data will
我在我的应用程序中使用谷歌地点。一切正常,但我只想从输入中删除或忽略国家/地区。 例如,如果输入“arc”我有 Arcachon, France Arcueil, France Archamps, F
在 firebase 中创建项目时,我错误地选择了错误的国家/地区。 正如您在这里看到的: 这个问题可以解决吗? (我知道项目 ID 无法更改,而且我喜欢我选择的 ID...) 最佳答案 如the d
我有一个名为“Login”的Hive表。它包含以下列: UserID | UserName | UserIP | UserCountry | Date 在特定的一天(当天的所有登录信息),我想找出用户
我正在使用谷歌地图和地理编码API来检索用户输入邮政编码/邮政编码后的纬度/经度,但我想将邮政编码/邮政编码限制为该用户所在的国家/地区,这样我就不会得到结果多个结果。 是否有一种方法可以使用 jqu
是否可以获得维基百科上所有有关系的国家、地区和城市的列表?我找不到任何适合此任务的 API。解析我需要的所有信息的最简单方法是什么?PS:我知道,我可以从其他数据源获取此信息。但我对维基百科感兴趣..
我正在用两种语言制作一个网站。是否可以检查用户位于哪个国家,以便我可以自动为用户切换语言?我的菜单中还有一个带有 _GET 的切换选项。我一开始有这段代码: if (isset($_GET['taal
这是我的 Firebase 数据库结构: 看起来就是这样。有国家和用户。如果用户访问过某个国家/地区,则该国家/地区将存储在该用户的“VisitedCountries”子项中。例如用户 Angelin
这个问题在这里已经有了答案: Convert latitude and longitude coordinates to country name in R (2 个回答) 关闭9年前. 我有一个带有
我目前正在开发我的第一个iOS应用(phonegap / cordova)。我想知道是否需要采取某些步骤,以使其不仅可用于德国应用程序商店(这是我的语言),还可以提供给国际应用程序? 最佳答案 不,你
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
背景 我目前正在开发一个向用户提问的 Android 应用程序。目前我正在创建用于提问的模块。现在我正在开发一个地形模块,它将能够向用户询问有关某个国家/地区的各种问题,并向他们显示。 问题 对于本模
我正在使用 Cloudera 的 TwitterSource for Flume。我想按国家/地区获取具有某些关键字的推文。当我想获取来自荷兰的推文时,我不知道该与什么进行比较。我有以下结果,但没有任
大多数 Web 应用程序都有一个位置字段,用户可以在其中输入自己选择的位置。 如何根据输入的位置将用户分类到不同的国家/地区。 例如,我使用了 users.xml 的 Stack Overflow 转
我是一名优秀的程序员,十分优秀!