简体   繁体   English

单击按钮时更改 CSS 样式?

[英]Change CSS style on button click?

I would like to change some of css style properties on div element when I click button, but I don't have that much experience nor I can find anything online to help me.我想在单击按钮时更改 div 元素上的一些 css 样式属性,但我没有那么多经验,也无法在网上找到任何帮助我的东西。 At the present moment this is how my code looks like.目前这就是我的代码的样子。

<button class="button1">Click</button>
<div class="popup_middle">
</div>
<div class="grayout">
</div>

and my CSS looks like this我的 CSS 看起来像这样

.grayout {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 99;
    visibility: hidden;
    background-color: #000;
    filter: alpha(opacity = 55);
    opacity:.80;
}

.popup_middle{
    position: fixed;
    background: url(../images/bg-food-order.jpg);
    border: none;
    top: 50%;
    left: 50%;
    width: 350px;
    height: 250px;
    margin-top: -125px;
    margin-left: -175px;
    z-index: 100;
    visibility: hidden;
}

Please could you pleas give me some advice or code sample with JS that is going to solve my problem.请您给我一些建议或使用 JS 的代码示例来解决我的问题。

Thanks in Advance , David提前致谢,大卫

Something like就像是

<script type='text/javascript'>

$('.button1').click(function() { $('.popup_middle').hide().css('color', 'blue'); });

</script>

Would hide that popup_middle div and set the text color to blue when you clicked on the button.当您单击按钮时,将隐藏 popup_middle div 并将文本颜色设置为蓝色。 click() is the event handler click() 是事件处理程序

You use getElementById which will look for elements with an id.您使用getElementById它将查找具有 id 的元素。 The div you are looking for has a class but no id.您正在寻找的 div 有一个类,但没有 id。 So when you run the code you have, nothing happens because the javascript cant find any element with an id of popup_middle .因此,当您运行您拥有的代码时,没有任何反应,因为 javascript 无法找到任何具有popup_middle idpopup_middle

To fix this you can change the div to have an Id of popup_middle and your CSS should have #popup_middle .要解决此问题,您可以将div更改为具有popup_middleId ,并且您的 CSS 应该具有#popup_middle

HTML: HTML:

<button onClick="document.getElementById('popup_middle').style.visibility='visible';">Cli‌ck</button>
<div id="popup_middle"></div>
<div class="grayout"></div>

CSS: CSS:

.grayout {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 99;
    visibility: hidden;
    background-color: #000;
    filter: alpha(opacity=55);
    opacity:.80;
}
#popup_middle {
    position: fixed;
    background: green;
    border: none;
    top: 50%;
    left: 50%;
    width: 350px;
    height: 250px;
    margin-top: -125px;
    margin-left: -175px;
    z-index: 100;
    visibility: hidden;
}

fiddle: http://jsfiddle.net/eLJYZ/2/小提琴: http : //jsfiddle.net/eLJYZ/2/

so if you want to add css class style to a text using a button:=>因此,如果您想使用按钮将 css 类样式添加到文本中:=>

          <script>
            function changed(){
            document.getElementById("exp").classList.add("the class style name")};
          </Script>
              <div id="exp">
                <button onclick=" changed()">try it</button>
                <p onclick="changed()">hello world</p>

              </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM