【发布时间】:2021-03-10 21:08:39
【问题描述】:
HTML
<html lang="en">
<head>
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="firstapp.js"></script>
</head>
<body>
<div id="buttons">
<h1>Hello</h1>
<h2>0</h2>
<button class="btn btn-primary" type="submit">Hello</button>
<button class="button2">count </button>
</div>
</body>
</html>
Javascript
let x = 0
function counter() {
x++;
document.querySelector('h2').innerHTML = x;
if (x % 10 === 0) {
alert(`counter is now ${x}`);
}
}
document.addEventListener("DOMContentLoaded", function() {
document.querySelector('.button2').onclick = counter; //This statement is for counter function
document.querySelector('.btn btn-primary').onclick = function() { //This is Hello Function
let heading1 = document.querySelector('h1');
if (heading1.innerHTML === "Hello!!") {
heading1.innerHTML = "Goodbye!!";
//document.querySelector('.btn btn-outline-secondary').innerHTML = 'goodbye';
} else {
heading1.innerHTML = "Hello!!";
//document.querySelector('.btn btn-outline-secondary').innerHTML = 'Hello!!';
}
};
})
当我使用本地 css 文件 Document.queryselector 时,它工作得很好,但是一旦我链接引导类,Java 脚本就无法识别任何引导类并给出错误“TypeError:无法设置属性'onclick'为空 在 HTML 文档。 "
【问题讨论】:
标签: javascript html css bootstrap-4