每當我嘗試更改此代碼中的內容時,我只更改按鈕和功能部分,但要么什么都沒有顯示,要么按鈕不起作用,當我單擊按鈕時,它應該顯示勵志評論,對于我的網站米制作,這里的代碼:
function myFunction() {
button.onclick = myFunction;
document.getElementById("mot").style.visibility = "visible";
}
<button onclick="myFunction()" style="visibility:visible;">Motivation</button>
<body id="mot" style="visibility:hidden;">
<p>It's harder too read code, then it is too write code</p>
<p>Practice makes better, not perfect</p>
<p>Good things take time, the better, the longer</p>
uj5u.com熱心網友回復:
你的 body 元素應該包含你所有的標記,而不僅僅是其中的一部分。另外,你
body沒有被關閉。我用 a
div來保存你的評論button.onclick = myFunction;不會像button未定義那樣作業。您還試圖在已經存在的事件處理程式中添加一個事件處理程式。
function myFunction()
{
document.getElementById("mot").style.visibility = "visible";
}
<button onclick="myFunction()" style="visibility:visible;">Motivation</button>
<div id="mot" style="visibility:hidden;">
<p>It's harder to read code than it is to write code</p>
<p>Practice makes better, not perfect</p>
<p>Good things take time, the better, the longer</p>
</div>
uj5u.com熱心網友回復:
在 jquery 中,您可以這樣做。
$(document).ready(function(){
$(".motivation").click(function(){
$("#mot").toggle();
});
});
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<button class="motivation" style="visibility:visible;">Motivation</button>
<div id="mot" style="display:none">
<p>It's harder too read code, then it is too write code</p>
<p>Practice makes better, not perfect</p>
<p>Good things take time, the better, the longer</p>
</div>
uj5u.com熱心網友回復:
您的 HTML 標記不符合標準。任何內容都應該在 body 標簽內變形。但是,您的代碼不需要button.onclick = myFunction;已經使用onclick按鈕事件屬性呼叫的函式。
不正確的標記可能會像這樣作業:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script>
function myFunction() {
//button.onclick = myFunction;
document.getElementById("mot").style.visibility = "visible";
}
</script>
</head>
<button onclick="myFunction()" style="visibility:visible;">Motivation</button>
<body id="mot" style="visibility:hidden;">
<p>It's harder too read code, then it is too write code</p>
<p>Practice makes better, not perfect</p>
<p>Good things take time, the better, the longer</p>
</body>
</html>
這是一個演示
要按照 Lee 上面的建議修復標記,請將激勵性評論放在 div 中。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script>
function myFunction() {
//button.onclick = myFunction;
document.getElementById("mot").style.visibility = "visible";
}
</script>
</head>
<body>
<button onclick="myFunction()" style="visibility:visible;">Motivation</button>
<div id="mot" style="visibility:hidden;">
<p>It's harder too read code, then it is too write code</p>
<p>Practice makes better, not perfect</p>
<p>Good things take time, the better, the longer</p>
</div>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/475266.html
