这个函数有什么问题?我无法让按钮出现,也无法让函数运行

What is wrong with this function? I can't get the button to appear and the function to run

本文关键字:函数 运行 按钮 问题 什么      更新时间:2023-09-26
    <script>
     function changeBgImage(){
    document.body.getElementbyId.style.backgroundImage = "url('colombia.jpg')";          "getElementbyId:'links'"}
    </script>
    </head>
<body>
<img src="worldcup.jpg" id="worldcup" height="200" width="600">
<img src="logo.jpg" id="logo1" height="200" width="200">
<img src="logo.jpg" id="logo2" height="200" width="200">
<p id="teams"> <big> <big> Teams that are in the World Cup </big></big>
 <br> <big> Group A: </big>
 <br> Brazil, Mexico, Croatia, Cameroon <br>
 <big> Group B: </big>
 <br> Netherlands, Chile, Spain, Australia <br>
 <big> Group C: </big>
 <br> Colombia, Ivory Coast, Japan, Greece <Br>
 <big> Group D: </big>
 <br> Costa Rica, Uruguay, Italy, England <br>
 <big> Group E: </big> <br>
 France, Ecuador, Switzerland, Honduras <Br>
 <big> Group F: </big> <br>
 Argentina, Nigeria, Iran, Bosnia
 <br>
 <big> Group G: </big> <br>
 Germany, United States, Ghana, Portugal <br>
 <big> Group H: </big> <br>
 Belgium, Algeria, Russia, Korea Republic 
 </p>
<p id="favorites"> 
<big> <big> <big> <big> My Favorites </big> </big> </big> <br>
  In this section of my webpage I would like to talk about my Favorite teams, 
  obviously I am rooting 100% for Colombia because it is my country, as well as Costa Rica. 
  Another obvious team I always root for is the US, because well...I'm also American. This World Cup has been 
  so great, I am extremely proud of all of the Latin American teams that have qualified for the next round and I 
  hope that a Latin American team makes it to the final. I am very excited because I will be in Miami for the final and I 
  am sure it will be very exciting. 
 </p>
    <img src="colombia.jpg" id="colombia" height="400px" width="400px">
<p id="soccerlove"> <big> <big> <big> Why I LOVE soccer! </big></big>
<br>
There are many many reasons why I love soccer. First of all, I was raised watching soccer games
and cheering on my family's favorite teams. It becomes a tradition,and when you are away from your family, you can watch a soccer game and feel at home. 
Soccer is such a beautiful sport, on the two videos on either side of this section you will find 2 reasons why I think soccer is so beautiful. On the left, a compilation 
of amazing goals, and on the right, the way that Colombian celebrate their goals! </p>
    <p id="goal1">
    <iframe width="350" height="315" src="http://www.youtube.com/embed/RGEm9T4Smts"     frameborder="0" allowfullscreen></iframe> </p>
    <p id="goal2">
    <iframe width="350" height="315" src="http://www.youtube.com/embed/FEJzRdU1viE" frameborder="0" allowfullscreen></iframe> </p>
<p id="links">
<h1> Who do you think will win? </h1>
<form>
<input type="button" name="links" id="links"
    value="Who will be the winner?!"
    onclick="changeBgImage">
    </p>
<form>

所以我需要函数"changeBgImage"运行…我怎样才能使它工作呢我想为它是一个按钮在我的页面底部。我已经尝试了各种方法,但我仍然不能让它工作,有人可以帮助我吗?

document.body.getElementbyId.style.backgroundImage = "url('colombia.jpg')";

getElementById是一个函数,需要一个参数。

getElementById("idofyourelementhere");

顺便说一下,我不知道你用"getElementbyId:'links'"做什么。它看起来像你试图使用对象语法没有设置到一个变量?或者使用适当的符号…

不应该在那里:

"getElementbyId:'links'"}

您的<form>未正确关闭;

getElementbyId应该以id作为参数

看起来你想改变页面背景

document.body.style.backgroundImage = "url('colombia.jpg')";  

演示:http://jsfiddle.net/jzmsb/

如果你想改变其他东西的背景,它需要一个id

<div id="changeme"></div>

然后用

作为目标
document.getElementById("changeme");

document.getElementById("changeme").style.backgroundImage = "url('colombia.jpg')";  
http://jsfiddle.net/jzmsb/1/