每當我單擊增量按鈕時,它都不會在 VS 代碼接線端子上顯示任何輸出。
它只顯示以下輸出“if ($?) { start Firefox pEOPLEcOUNTERapp.htm }”只需啟動 Firefox 瀏覽器,當我點擊增量按鈕時 --> 沒有輸出
代碼
<!DOCTYPE html>
<html lang="en">
<head>
<link rel = "stylesheet" href = "pEOPLEcOUNTERapp.css">
</head>
<body>
<h1> People Entered</h1>
<h2 id="count-el">0</h2>
<script src="pEOPLEcOUNTERapp.js">
// can write javascript
</script>
<button id="increment-btn" onclick="increment()">increment</button>
</body>
</html>
function increment(){ //javascript code
console.log("THE BUTTON WAS clicked")
}
JavaScript 代碼:
function increment()
console.log("THE BUTTON WAS clicked")
}
uj5u.com熱心網友回復:
試試這樣:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="increment-btn" onclick="increment()">increment</button>
</body>
<script>
function increment() {
console.log("THE BUTTON WAS clicked");
}
</script>
</html>
您必須在檔案末尾添加腳本。
uj5u.com熱心網友回復:
這應該可以作業(沒有單獨的 js 檔案)
<!DOCTYPE html>
<html lang="en">
<head>
<link rel = "stylesheet" href = "pEOPLEcOUNTERapp.css">
</head>
<body>
<h1> People Entered</h1>
<h2 id="count-el">0</h2>
<script>
function increment(){
console.log("THE BUTTON WAS clicked")
}
</script>
<button id="increment-btn" onclick="increment()">increment</button>
</body>
</html>
uj5u.com熱心網友回復:
聽起來您想更新該count-el元素中的計數。如果是這種情況,您確實需要defer腳本或將腳本呼叫添加到</body>標記之前,以確保 DOM 已加載,并且您的腳本可以處理這些元素。
這個例子消除了對行內 JS 的需要。它快取元素,然后將偵聽器添加到按鈕元素。單擊它時,它會呼叫increment更新元素文本內容的函式count-el。
const count = document.querySelector('#count-el');
const btn = document.querySelector('#increment-btn');
btn.addEventListener('click', increment, false);
function increment() {
count.textContent = Number(count.textContent) 1;
}
<h1> People Entered</h1>
<h2 id="count-el">0</h2>
<button id="increment-btn">increment</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470441.html
標籤:javascript html 按钮
