如果跨度id值為空,如何隱藏標簽?我需要隱藏一個結果,但我找不到解決方案。 如果這& lt輸入類型= & quot文本& quotid = & quotcap & quotname = & quotcap & quotplaceholder = & quot名稱& quot/& gt;值在index.html為空,則不顯示該級別& lt標簽id = & quott1w _ 1 & quot& gt名稱& lt/label & gt;在result.html 這是我的代碼
index.HTML
<html>
<head>
<title>
Input Form
</title>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<form action="result.html" method="POST">
<input type="text" id="cno" name="cno" placeholder="Count no.." autofocus required />
<input type="text" id="cap" name="cap" placeholder=" Name"/>
<input type="submit" onclick="handleSubmit()"/>
</form>
</body>
</html>
索引. js
function handleSubmit () {
const cno = document.getElementById('cno').value;
const cap = document.getElementById('cap').value;
// to set into local storage
/*localStorage.setItem("CNO", cno);
localStorage.setItem("CAP", cap);*/
sessionStorage.setItem("CNO", cno);
sessionStorage.setItem("CAP", cap);
return;
}
result.html
<!DOCTYPE html>
<html lang="en">
<script type="text/javascript" src="result.js" ></script>
<body style="margin: 100px;">
<span>: 6051670/S/23-24/210420230<span id="result-cno" /></span>/PS
<br>
<label id="t1w_1">Name</label>
<span id="result-cap"/></span>
</body>
</html>
結果. js
window.addEventListener('load', () => {
// Via Query parameters - GET
/* const params = (new URL(document.location)).searchParams;
const cno = params.get('cno');
const cap = params.get('cap');*/
// Via local Storage
/*const cno = localStorage.getItem('CNO');
const cap = localStorage.getItem('CAP');*/
const cno = sessionStorage.getItem('CNO');
const cap = sessionStorage.getItem('CAP');
document.getElementById('result-cno').innerHTML = cno;
document.getElementById('result-cap').innerHTML = cap;
})
如果這& lt輸入類型= & quot文本& quotid = & quotcap & quotname = & quotcap & quotplaceholder = & quot名稱& quot/& gt;值為空,則不顯示該級別& lt標簽id = & quott1w _ 1 & quot& gt名稱& lt/label & gt;在result.html
有很多方法可以做到這一點。
你可以創建一個CSS類,使用display none來隱藏元素。然后,如果值是隱藏的,使用javascript在元素上切換該類。
let cap = "";
let t1w_1 = document.querySelector("#t1w_1");
t1w_1.classList.toggle("hide",(cap == ""))
.hide{display:none}
<label id="t1w_1">Name</label>
上一篇如何用CSS替換文本?
下一篇vue 設置獲取焦點