我想使用onclick()将我的网站翻译成法语。 然后我想用同样的方法用英语翻译。
这是代码。任何的想法 ?谢谢!
<html>
<head><title>Test Translate</title>
<script language="JavaScript">
function TranslateToFrench()
{
MyText.innerText = "Ceci est un texte en Français.";
languageBox.innerText = "English";
}
function TranslateToEnglish()
{
MyText.innerText = "This is a text in English.";
languageBox.innerText = "Français";
}
</script>
</head>
<body>
<h3 id="languageBox" align="right" onclick="TranslateToFrench()">Français</h3>
<p id="MyText">This is a text in English.</p>
</body>
</html>
答案 0 :(得分:2)
<html>
<head><title>Test Translate</title>
<script>
var app = 0;
function Translate()
if(app == 0){
app = 1;
TranslateToFrench();
}else{
app = 0;
TranslateToEnglish();
}
}
function TranslateToFrench()
{
MyText.innerText = "Ceci est un texte en Français.";
languageBox.innerText = "English";
}
function TranslateToEnglish()
{
MyText.innerText = "This is a text in English.";
languageBox.innerText = "Français";
}
</script>
</head>
<body>
<h3 id="languageBox" align="right" onclick="Translate()">Français</h3>
<p id="MyText">This is a text in English.</p>
</body>
</html>
答案 1 :(得分:2)
只需检查h3
元素innerText
并在该调用中检查正确的功能
尝试:
function TranslateToFrench()
{
if(languageBox.innerText==="Français"){
MyText.innerText = "Ceci est un texte en Français.";
languageBox.innerText = "English";}
else{
TranslateToEnglish();
}
}
function TranslateToEnglish()
{
MyText.innerText = "This is a text in English.";
languageBox.innerText = "Français";
}