我不知道我在这里做错了什么,但是点击我
按钮不起作用,为什么?我想在第一个单击我更改第一个段落的颜色,第二个单击我更改第二段的颜色。无论如何,点击都不起作用。我必须查看我的功能,但看不出我做错了什么。也许需要创建其他功能才能使它们工作?仅此而已,按钮单击对该代码不起作用。
<!DOCTYPE html>
<html>
<head>
<h1>Tv Shows</h1>
<div class="bg-image img1">
<p id="demo">My Top 5 Tv shows,is Prison Break,Wayward Pines,Mentalist,Lost,Usa shooter</p>
<button onclick="myFunction()">click me</button>
</div>
<div class="bg-image img2">
<p id="demo1">My Top 5 movies,is Run all Night ,Asterix and Obelix,,Lost,Usa
shooter</p>
<button onclick="myFunction()">click me</button>
</div>
<style>
body, html {
height: 100%;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
* {
box-sizing: border-box;
}
.bg-image {
/* Full height */
height: 50%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
/* Images used */
.img1 { background-image: url("https://images.pexels.com/photos/1115804/pexels-photo-1115804.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); }
.img2 { background-image: url("https://images.pexels.com/photos/3989816/pexels-photo-3989816.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); }
/* Position text in the middle of the page/image */
.bg-text {
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0, 0.4); /* Black w/opacity/see-through */
color: white;
font-weight: bold;
font-size: 80px;
border: 10px solid #f1f1f1;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 2;
width: 300px;
padding: 20px;
text-align: center;
}
function myFunction() {
var x = document.getElementById("demo");
x.style.fontSize = "25px";
x.style.color = "red";
}
function mySec() {
var x = document.getElementById("demo1");
x.style.fontSize = "25px";
x.style.color = "green";
}
</style>
</head>
<body>
</body>
</html>
最佳答案
首先,代码中使用的语法不正确。最好的方法是使用单一函数并传递 <p>
的 id标签。以下是更新后的代码
<html>
<head>
<style>
body,
html {
height: 100%;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
* {
box-sizing: border-box;
}
.bg-image {
/* Full height */
height: 50%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
/* Images used */
.img1 {
background-image: url("https://images.pexels.com/photos/1115804/pexels-photo-1115804.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
}
.img2 {
background-image: url("https://images.pexels.com/photos/3989816/pexels-photo-3989816.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
}
/* Position text in the middle of the page/image */
.bg-text {
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/opacity/see-through */
color: white;
font-weight: bold;
font-size: 80px;
border: 10px solid #f1f1f1;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 2;
width: 300px;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<h1>Tv Shows</h1>
<div class="bg-image img1">
<p id="demo">My Top 5 Tv shows,is Prison Break,Wayward Pines,Mentalist,Lost,Usa shooter</p>
<button onclick="myFunction('demo')">click me</button>
</div>
<div class="bg-image img2">
<p id="demo1">My Top 5 movies,is Run all Night ,Asterix and Obelix,,Lost,Usa shooter
</p>
<button onclick="myFunction('demo1')">click me</button>
</div>
<script>
function myFunction(id) {
var x = document.getElementById(id);
x.style.fontSize = "25px";
x.style.color = "red";
}
</script>
</body>
</html>
关于javascript - 点击我按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61445706/