gpt4 book ai didi

javascript - 根据元素内的类显示函数 - Javascript vanilla

转载 作者:行者123 更新时间:2023-11-28 16:51:49 26 4
gpt4 key购买 nike

TLDR 我不知道如何编写函数 onclick("sl") 显示类“sl”的内容&&类“lng”隐藏类“lng”的其他所有内容&& 类“(en, de, it, hr, ru)”

你好,

我正在制作一个多语言注册页面。我目前陷入困境,因为我不知道如何编写/修改一个函数来检测一个元素是否有两个特定的类(类 lng 和类 s​​l、en、de、it、hr 或 ru),并且取决于该组合显示内容(display: none;display: inline-block;)。

此外,我希望占位符的内容根据所选语言进行更改。

编辑工作display: none;/display: inline;

修改后的JS

        const lngs = document.getElementsByClassName("lng");

function init() {
for (var i = 0; i < lngs.length; i++) {
lngs[i].style.display = "none";
}
}

init();

function setLanguage(language) {

debugger
console.log(language);
for (var i = 0; i < lngs.length; i++) {
if (lngs[i].className.includes(language)) {
lngs[i].style.display = "inline";
} else {
lngs[i].style.display = "none";
}
}
}

编辑后的 ​​HTML

<html>
<head>
</head>
<body>
<img class="background-image" />
<header>
<div class="top-nav-logo">
<img class="top-logo" src="assets/Logo_Sava.jpg">
</div>
<div class="top-language">
<div>
<input type="image" id="slo" class="zastave" src="assets/drzave/Slo.png" onclick="setLanguage('sl')">
<input type="image" id="ang" class="zastave" src="assets/drzave/Ang.png" onclick="setLanguage('en')">
<input type="image" id="nem" class="zastave" src="assets/drzave/Nem.png" onclick="setLanguage('de')">
<input type="image" id="ita" class="zastave" src="assets/drzave/Ita.png" onclick="setLanguage('it')">
<input type="image" id="hrv" class="zastave" src="assets/drzave/Hrv.png" onclick="setLanguage('hr')">
<input type="image" id="rus" class="zastave" src="assets/drzave/Rus.png" onclick="setLanguage('ru')">
</div>
</div>
</header>

<div class="pasica">
<img src=""/>
</div>
<div class="center-div">
<div class="registerTitle">
<div class="lng si">
Slo - Register
</div>
<div class="lng en">
Eng - Register
</div>
<div class="lng de">
Deu - Register
</div>
<div class="lng it">
Ita - Register
</div>
<div class="lng hr">
Hrv - Register
</div>
<div class="lng ru">
Rus - Register
</div>
</div>
<div class="registerForm">
<form accept-charset="utf-8" name="form" onsubmit="return false;" method="post" id="form">
<input type="text" name="fname" id="fname" required maxlength="50" minlength="1" placeholder="Janez"
onfocus="this.placeholder = ''" onblur="this.placeholder = 'Janez'" /><br />
<input type="text" name="lname" id="lname" required maxlength="50" minlength="1" placeholder="Novak"
onfocus="this.placeholder = ''" onblur="this.placeholder = 'Novak'" /><br />

<span class="lng sl radioS">Spol: </span>
<span class="lng en radioS">Gender: </span>
<span class="lng de radioS">Spol DE: </span>
<span class="lng it radioS">Gendero IT: </span>
<span class="lng hr radioS">Spolov: </span>
<span class="lng ru radioS">секс: </span><br /><br />
<div class="radioC"><label><input type="radio" name="spol" value="M"> <span class="lng sl">Moški SI</span>
<span class="lng en">Moški EN</span>
<span class="lng de">Moški DE</span>
<span class="lng it">Moški IT</span>
<span class="lng hr">Moški HR</span>
<span class="lng ru">Moški RU</span>
</label></div><br />
<div class="radioC"><label><input type="radio" name="spol" value="Z"> <span class="lng sl">Ženski SI</span>
<span class="lng en">Ženski EN</span>
<span class="lng de">Ženski DE</span>
<span class="lng it">Ženski IT</span>
<span class="lng hr">Ženski HR</span>
<span class="lng ru">Ženski RU</span>
</label></div><br />
<div class="radioC"><label><input type="radio" name="spol" value="O"> <span class="lng sl">Ostalo SI</span>
<span class="lng en">Ostalo EN</span>
<span class="lng de">Ostalo DE</span>
<span class="lng it">Ostalo IT</span>
<span class="lng hr">Ostalo HR</span>
<span class="lng ru">Ostalo RU</span>
</label></div><br /><br />

<input type="email" name="email" id="email" autofocus="autofocus" required placeholder="moj@email.com"
onfocus="this.placeholder = ''" onblur="this.placeholder = 'moj@email.com'" />
<br />
<input type="button" value="Submit" id="submit_ok" name="submit_ok" /> <br />
</form>
</div>
</div>
</body>
</html>

HTML

<!DOCTYPE html >
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles/main.css">
<link rel="stylesheet" type="text/css" href="styles/language.css">
<script type="text/javascript" src="js/myScript.js"></script>
<script type="text/javascript" src="js/language.js"></script>
<meta charset="utf-8">
<title>Registration</title>
</head>
<body>
<img class="background-image" />
<header>
<div class="top-nav-logo">
<img class="top-logo" src="assets/Logo_Sava.jpg">
</div>
<div class="top-language">
<div>
<input type="image" id="slo" class="zastave" src="assets/drzave/Slo.png" onclick="setLanguage('sl')">
<input type="image" id="ang" class="zastave" src="assets/drzave/Ang.png" onclick="setLanguage('en')">
<input type="image" id="nem" class="zastave" src="assets/drzave/Nem.png" onclick="setLanguage('de')">
<input type="image" id="ita" class="zastave" src="assets/drzave/Ita.png" onclick="setLanguage('it')">
<input type="image" id="hrv" class="zastave" src="assets/drzave/Hrv.png" onclick="setLanguage('hr')">
<input type="image" id="rus" class="zastave" src="assets/drzave/Rus.png" onclick="setLanguage('ru')">
</div>
</div>
</header>
<div class="center-div">
<div class="registerTitle">
<div class="lng" class="sl">
Slo - Register
</div>
<div class="lng" class="en">
Eng - Register
</div>
<div class="lng" class="de">
Deu - Register
</div>
<div class="lng" class="it">
Ita - Register
</div>
<div class="lng" class="hr">
Hrv - Register
</div>
<div class="lng" class="ru">
Rus - Register
</div>
</div>
<div class="registerForm">
<form accept-charset="utf-8" name="form" onsubmit="return false;" method="post" id="form">
<input type="text" name="fname" id="fname" required maxlength="50" minlength="1" placeholder="Janez"
onfocus="this.placeholder = ''" onblur="this.placeholder = 'Janez'" /><br />
<input type="text" name="lname" id="lname" required maxlength="50" minlength="1" placeholder="Novak"
onfocus="this.placeholder = ''" onblur="this.placeholder = 'Novak'" /><br />

<span class="lng" class="sl" class="radioS">Spol: </span>
<span class="lng" class="en" class="radioS">Gender: </span>
<span class="lng" class="de" class="radioS">Spol DE: </span>
<span class="lng" class="it" class="radioS">Spol IT: </span>
<span class="lng" class="hr" class="radioS">Spol HR: </span>
<span class="lng" class="ru" class="radioS">Spol RU: </span><br /><br />
<div class="radioC"><label><input type="radio" name="spol" value="M"> <span class="lng" class="sl">Moški SI</span>
<span class="lng" class="en">Moški EN</span>
<span class="lng" class="de">Moški DE</span>
<span class="lng" class="it">Moški IT</span>
<span class="lng" class="hr">Moški HR</span>
<span class="lng" class="ru">Moški RU</span>
</label></div><br />
<div class="radioC"><label><input type="radio" name="spol" value="Z"> <span class="lng" class="sl">Ženski SI</span>
<span class="lng" class="en">Ženski EN</span>
<span class="lng" class="de">Ženski DE</span>
<span class="lng" class="it">Ženski IT</span>
<span class="lng" class="hr">Ženski HR</span>
<span class="lng" class="ru">Ženski RU</span>
</label></div><br />
<div class="radioC"><label><input type="radio" name="spol" value="O"> <span class="lng" class="sl">Ostalo SI</span>
<span class="lng" class="en">Ostalo EN</span>
<span class="lng" class="de">Ostalo DE</span>
<span class="lng" class="it">Ostalo IT</span>
<span class="lng" class="hr">Ostalo HR</span>
<span class="lng" class="ru">Ostalo RU</span>
</label></div><br /><br />

<input type="email" name="email" id="email" autofocus="autofocus" required placeholder="moj@email.com"
onfocus="this.placeholder = ''" onblur="this.placeholder = 'moj@email.com'" />
<br />
<input type="button" value="Submit" id="submit_ok" name="submit_ok" /> <br />
</form>
</div>
</div>
</body>
</html>

Javascript - 我目前拥有的和不起作用的

        let fname = document.getElementById("fname");
let lname = document.getElementById("lname");
let email = document.getElementById("email");

function setInputs(language) {
var jeziki;
var i;
jeziki = document.getElementsByClassName("lng");
console.log(jeziki);
for (var i=0;i<jeziki.length;i++){
if (jeziki[i].getElementsByClassName === "sl") {
fname.setAttribute('placeholder', 'Janez');
lname.setAttribute('placeholder', 'Novak');
email.setAttribute('placeholder', 'moj@email.si');
} else if (jeziki[i].getElementsByClassName === "en"){
fname.setAttribute('placeholder', 'John');
lname.setAttribute('placeholder', 'Doe');
email.setAttribute('placeholder', 'my@email.com');
} else if (jeziki[i].getElementsByClassName === "de"){
fname.setAttribute('placeholder', 'Comisar');
lname.setAttribute('placeholder', 'Rex');
email.setAttribute('placeholder', 'Rex@hou.dre');
} else if (jeziki[i].getElementsByClassName === "it"){
fname.setAttribute('placeholder', 'Mak');
lname.setAttribute('placeholder', 'Aron');
email.setAttribute('placeholder', 'forza@email.it');
} else if (jeziki[i].getElementsByClassName === "hr"){
fname.setAttribute('placeholder', 'Mirko');
lname.setAttribute('placeholder', 'Mirkic');
email.setAttribute('placeholder', 'nase@more.hr');
} else if (jeziki[i].getElementsByClassName === "ru"){
fname.setAttribute('placeholder', 'Milan');
lname.setAttribute('placeholder', 'Rusky');
email.setAttribute('placeholder', 'rus@go.ru');
} else {
fname.setAttribute('placeholder', 'John');
lname.setAttribute('placeholder', 'Doe');
email.setAttribute('placeholder', 'my@email.com');
}
}
}

Javascript - 有效,但我必须删除 ID,并且页面的设置随后有所不同(每种语言都有自己的形式)。

        function setLanguage(language) {
var lngs;
var i;
lngs = document.getElementsByClassName("lng"); //array vseh lng-jev
console.log(lngs);
for (var i=0;i<lngs.length;i++){
if (lngs[i].getElementsByClassName(language) !== language) {
lngs[i].style.display="none";
} else {
lngs[i].style.display="inline-block";
}
}
}

最佳答案

做到这一点的方法是首先隐藏所有内容,然后显示应该可见的元素

var hide = ".lng";
var show = ".lng.sl";

document.querySelectorAll(hide).forEach(function() {
this.style.display = "none";
});
document.querySelectorAll(show).forEach(function() {
this.style.display = "inline-block";
});

关于javascript - 根据元素内的类显示函数 - Javascript vanilla,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59863347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com