简体   繁体   English

使用JS更改Div的颜色

[英]Changing Colour of a Div with JS

So, I have a simple onclick function set up that is supposed to change the background of a div. 因此,我设置了一个简单的onclick函数,该函数应该更改div的背景。 Here is my JS code; 这是我的JS代码;

var colour = document.getElementById("backgroundColour");

function changeToColour1() {
    colour.style.backgroundColor = "#47535F";
}

function changeToColour2() {
    colour.style.backgroundColor = "#82A3B2";
}

Here is my HTML code; 这是我的HTML代码;

<button id="colour1" class="colours" onclick="changeToColour1()"></button>

<button id="colour2" class="colours" onclick="changeToColour2()"></button>

<div id="backgroundColour"></div>

How would I get the div to change its colour when the button is clicked? 单击按钮后,如何使div更改其颜色?

a much cleaner way of doing this would be to just use javascript to add classes to your elements, and let CSS control the colors. 一种更干净的方法是使用javascript将类添加到元素中,然后让CSS控制颜色。


$('#colour1').on('click',function(){
  $('#backgroundColour').removeClass('colour1 colour2').addClass('colour1');
});

$('#colour2').on('click',function(){
  $('#backgroundColour').removeClass('colour1 colour2').addClass('colour2');
});

and in your CSS put 并放在您的CSS中


#backgroundColour.colour1 {
  background-color: #47535F;
}

#backgroundColour.colour2 {
  background-color: #82A3B2;
}
  • Give the Div a proper width and height or some content, otherwise the div cannot be seen. 给Div适当的宽度和高度或一些内容,否则将看不到div。
  • Avoid inline event handlers and use DOM3 event handlers. 避免使用内联事件处理程序,并使用DOM3事件处理程序。
  • You can also use data-* attributes to store the colors instead of having a seperate function for each button. 您也可以使用data- *属性来存储颜色,而不是为每个按钮使用单独的功能。

HTML HTML

<button id="colour1" class="colours" data-color="#47535F" >Click me</button>

<button id="colour2" class="colours" data-color="#82A3B2">Click me</button>

<div id="backgroundColour">fff</div>

JS JS

var colourDiv = document.getElementById("backgroundColour");

var colours = document.getElementsByClassName('colours');

for(var i=0; i< colours.length; i++) {
    colours[i].addEventListener('click', function() {
         var color = this.getAttribute('data-color');
         colourDiv.style.backgroundColor = color;
    });
}

Check Fiddle 检查小提琴

first and foremost i recommend you to use JQuery for the task you are trying to accomplish, im recommending you do for the simple fact JQuery is a simple straight forward library for javascript that makes our lives as coders a lot more simple, now that im done saying that here is the code. 首先,我建议您使用JQuery来完成您要完成的任务,即时消息建议您这样做,因为JQuery是一个简单的javascript直截了当的库,使我们作为编码员的生活变得更加简单,说这是代码。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
    <title>...</title>

    <style type="text/css">
        html, body{
            width:auto;
            height:auto;
        }
        #changing{ /*this is the css for our javascript*/
            margin:0px;
            padding:0px;
            width:100%;
            height:100%;
            position:absolute;
            top:0px;
            left:0px;
            right:0px;
            bottom:0px;
        }
        #wrap{ /*this is to keep the browse menu that has your buttons from disappearing below the changing div*/
            margin:0px;
            padding:0px;
            width:100%;
            height:40px;;
            background-color:#000000;
            border-bottom:5px solid #D8D8D8;
            border-radius:0px;
            position:fixed;
            top:0px;
            left:0px;
            z-index:10;
        }
        #button1, #button2{
            margin:0px;
            padding:0px;
            width:auto;
            height:auto;
            position:fixed;
        }
        #button1{
            top:0px;
            left:0px;
        }
        #button2{
            top:0px;
            right:0px;
        }
    </style>

    <script rel="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
    <div id="body-container">
        <div id="wrap">
            <input type="button" id="button1" value="click me" />
            <input type="button" id="button2" value="click me 2" />
        </div>
        <div id="changing">
        </div>
    </div>
    <script>
        $("#button1").click(function(){
            $("#changing").css("background-color", "#47535F");//changes the color of the background to #47535F
        })
        $("#button2").click(function(){
            $("#changing").css("background-color", "#82A3B2");//changes the color of the background to #82A3B2
        })
    </script>
</body>
</html>

in this code we use inline css and javascript with the JQuery library, we first start the document how we should always start a html document by adding the DOCTYPE and our meta tags with the attributes content-type and content-language we also add a title. 在此代码中,我们将内联css和javascript与JQuery库一起使用,我们首先通过添加DOCTYPE和具有属性content-type和content-language的meta标签来开始文档如何始终启动html文档的过程,同时还添加了标题。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
    <title>...</title>
</head>
<body>

</body>
</html>

next we add our our script to import the JQuery library, i usually go with the google hosted version because its usually up and running and i dont have to constantly update it , google does that for you, the only thing you need to do is add the new url in the src to replace the old version of JQuery. 接下来,我们添加脚本以导入JQuery库,我通常会使用google托管版本,因为它通常可以正常运行,并且我不必不断地对其进行更新,那么google会为您做到这一点,您唯一需要做的就是添加src中的新URL替换旧版本的JQuery。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
    <title>...</title>

    <script rel="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>

</body>
</html>

next we add our html, in our html we added two buttons and placed them in a div called wrap and added our changing div and placed both the changing div and the wrap div into a div called body-container. 接下来,我们添加html,在我们的html中,我们添加了两个按钮,并将它们放置在称为wrap的div中,并添加了更改的div,并将更改的div和wrap div都放置到称为body-container的div中。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
    <title>...</title>

    <script rel="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
    <div id="body-container">
        <div id="wrap">
            <input type="button" id="button1" value="click me" />
            <input type="button" id="button2" value="click me 2" />
        </div>
        <div id="changing">
        </div>
    </div>
</body>
</html>

now time to add the icing to the cake with our script and css, here we added our css to determine the style of our page we made ours so that the browse menu were are buttons are located scroll with the page and are always onto, now onto the script, we made our script so that if the button is pressed it will change the background of the div body-container. 现在是时候使用脚本和CSS添加锦上添花了,在这里我们添加CSS来确定我们制作的页面样式,以便浏览菜单是按钮位于页面上并始终滚动到现在在脚本上,我们制作了脚本,因此如果按下按钮,它将更改div body-container的背景。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
    <title>...</title>

    <style type="text/css">
        html, body{
            width:auto;
            height:auto;
        }
        #changing{ /*this is the css for our javascript*/
            margin:0px;
            padding:0px;
            width:100%;
            height:100%;
            position:absolute;
            top:0px;
            left:0px;
            right:0px;
            bottom:0px;
        }
        #wrap{ /*this is to keep the browse menu that has your buttons from disappearing below the changing div*/
            margin:0px;
            padding:0px;
            width:100%;
            height:40px;;
            background-color:#000000;
            border-bottom:5px solid #D8D8D8;
            border-radius:0px;
            position:fixed;
            top:0px;
            left:0px;
            z-index:10;
        }
        #button1, #button2{
            margin:0px;
            padding:0px;
            width:auto;
            height:auto;
            position:fixed;
        }
        #button1{
            top:0px;
            left:0px;
        }
        #button2{
            top:0px;
            right:0px;
        }
    </style>

    <script rel="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
    <div id="body-container">
        <div id="wrap">
            <input type="button" id="button1" value="click me" />
            <input type="button" id="button2" value="click me 2" />
        </div>
        <div id="changing">
        </div>
    </div>
    <script>
        $("#button1").click(function(){
            $("#changing").css("background-color", "#47535F");//changes the color of the background to #47535F
        })
        $("#button2").click(function(){
            $("#changing").css("background-color", "#82A3B2");//changes the color of the background to #82A3B2
        })
    </script>
</body>
</html>

i hoped this helped, for any information on JQuery i recommend you go visit their online documents, they have a lot of useful info there, here is the link 我希望这会有所帮助,有关JQuery的任何信息,我建议您访问其在线文档,那里有很多有用的信息,这是链接

http://api.jquery.com/ http://api.jquery.com/

you can also check out w3chools they have a lot of good stuff on JQuery as well as many other programming languages, here is the link 您还可以查看w3chools,它们在JQuery以及许多其他编程语言上都有很多不错的东西,这是链接

http://www.w3schools.com/ http://www.w3schools.com/

best of luck with your project or learning the javascript language (= 祝您在项目中运气佳或学习javascript语言(=

JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> - JavaScript for changing a <div> colour from a button press

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery更改Div的颜色 - JQuery changing colour of Div div颜色在.animate()中不变 - div colour not changing in .animate() 使用jQuery更改div的颜色 - Changing colour of a div using jQuery javascript遍历div更改背景颜色 - javascript iterate through div changing the background colour 使用 React 中的按钮单击更改 div 颜色样式 - Changing div colour styling with button click in React 使用jQuery更改div的背景色 - Using jQuery to changing background colour of a div JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> - JavaScript for changing a <div> colour from a button press 使用js mouseover和mouseout更改背景颜色 - Changing background colour using js mouseover and mouseout 使用js动态更改背景图像的颜色 - changing colour of a background image with js on the fly 使用php和js更改表格单元格中文本的颜色 - Changing colour of text in table cell with php and js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM