我如何改变div HTML标签通过按钮onclick事件使用JavaScript

iibxawm4  于 2023-09-28  发布在  Java
关注(0)|答案(6)|浏览(122)
<nav class="navbar navbar-default" style="...">
    <div class="container" id="main_container" style="margin-top: -80px">
        <div>
            <div class="row">
                <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id6 }}" target="_blank">{{ id6 }}</a></div>
                <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating6 }}</div>
                <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description6 }}</marquee></div>
                <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date6 }}</div>
            </div>
            <div class="row">
                <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id7 }}" target="_blank">{{ id7 }}</a></div>
                <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating7 }}</div>
                <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description7 }}</marquee></div>
                <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date7 }}</div>
            </div>
            <div class="row" style="margin-top: 5px">
                <div class="col-xs-0 col-md-5">
                </div>
                <div class="col-xs-0 col-md-2" align="center">
                    <button class="btn btn-xs btn-default" type="button" id="my_button">History</button>
                </div>
                <div class="col-xs-0 col-md-5"></div>
            </div>
            <div class="row" style="margin-top: 5px">
                <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id8 }}" target="_blank">{{ id8 }}</a></div>
                <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating8 }}</div>
                <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description8 }}</marquee></div>
                <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date8 }}</div>
            </div>
        </div>
    </div>
</nav>

我有一个简单的标题HTML div结构与按钮id="my_button"。在按钮事件onclick上,第一个带有id="main_container"的div标签应该改变css指令,特别是margin-top值从-80px改为0px,当margin-top值等于0px时,应该改为-80px
我是Web开发新手。我想我需要一个带有OnClick事件处理程序的JavaScript脚本函数。

cunj1qz1

cunj1qz11#

使用下面的函数通过使用JavaScript更改margin-top

<script type="text/javascript">
$(function() {
    $('#my_button').click(function() {
         var margin = $('#main_container').css('margin-top');
         var newMargin = (margin=='-80px')?'0px':'-80px';
         $('#main_container').css('margin-top',newMargin);
    });
});
</script>
bxgwgixi

bxgwgixi2#

  • 您应该使用style=“margin:xxx%”将边距添加到div中
    您可以使用此JavaScript代码在单击按钮时更改它:
function MoveDiv() {
    var e = document.getElementById("YOUR_DIV_id");
    if (e.style.marginTop == '-80px')
    {
        e.style.marginTop = "0px";
    }
    else 
    {
        e.style.marginTop = "-80px";
    }
}

然后你可以简单地用一个OnClick函数将它调用到一个按钮中,类似于这样:<button onClick="MoveDiv()">Click me</button>

o2g1uqev

o2g1uqev3#

在按钮单击时调用JavaScript函数并验证marginTop值。

HTML

<button class="btn btn-xs btn-default" type="button" id="my_button" onclick="test()">History</button>

JS

function test() {
    var marginValue = document.getElementById('main_container').style.marginTop;
    if(marginValue == "-80px") {
      document.getElementById('main_container').style.marginTop = "0px";
    } else {
      document.getElementById('main_container').style.marginTop = "-80px";
    }
   }

DEMO

xpszyzbs

xpszyzbs4#

var button = document.querySelector("#my_button");
var container = document.querySelector("#main_container");

button.addEventListener("click", function () {
  container.style.marginTop = "0px"
})
<nav class="navbar navbar-default" style="...">
    <div class="container" id="main_container" style="margin-top: -80px">
        <div>
            <div class="row">
                <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id6 }}" target="_blank">{{ id6 }}</a></div>
                <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating6 }}</div>
                <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description6 }}</marquee></div>
                <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date6 }}</div>
            </div>
            <div class="row">
                <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id7 }}" target="_blank">{{ id7 }}</a></div>
                <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating7 }}</div>
                <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description7 }}</marquee></div>
                <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date7 }}</div>
            </div>
            <div class="row" style="margin-top: 5px">
                <div class="col-xs-0 col-md-5">
                </div>
                <div class="col-xs-0 col-md-2" align="center">
                    <button class="btn btn-xs btn-default" type="button" id="my_button">History</button>
                </div>
                <div class="col-xs-0 col-md-5"></div>
            </div>
            <div class="row" style="margin-top: 5px">
                <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id8 }}" target="_blank">{{ id8 }}</a></div>
                <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating8 }}</div>
                <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description8 }}</marquee></div>
                <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date8 }}</div>
            </div>
        </div>
    </div>
</nav>

但是我推荐使用transform来获得更好的性能。举例来说:
之前:transform: translateY(-80px)。之后:transform: translateY(0)

tzcvj98z

tzcvj98z5#

请尝试以下代码,我删除了你的内联样式并添加了css类:

<style>
#main_container { margin-top:0;}
#main_container.margin_top { margin-top:-80px;}
</style>

<nav class="navbar navbar-default" style="...">
<div class="container" id="main_container">
    <div>
        <div class="row">
            <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id6 }}" target="_blank">{{ id6 }}</a></div>
            <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating6 }}</div>
            <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description6 }}</marquee></div>
            <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date6 }}</div>
        </div>
        <div class="row">
            <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id7 }}" target="_blank">{{ id7 }}</a></div>
            <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating7 }}</div>
            <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description7 }}</marquee></div>
            <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date7 }}</div>
        </div>
        <div class="row" style="margin-top: 5px">
            <div class="col-xs-0 col-md-5">
            </div>
            <div class="col-xs-0 col-md-2" align="center">
                <button class="btn btn-xs btn-default" type="button" id="my_button">History</button>
            </div>
            <div class="col-xs-0 col-md-5"></div>
        </div>
        <div class="row" style="margin-top: 5px">
            <div class="col-xs-0 col-md-2" style="background-color: #f8f8f8;text-align: center;"><a href="http://cve.circl.lu/api/cve/{{ id8 }}" target="_blank">{{ id8 }}</a></div>
            <div class="col-xs-0 col-md-1" style="background-color: #f5f4f5;text-align: center;">{{ rating8 }}</div>
            <div class="col-xs-0 col-md-6" style="background-color: #F8F8F8"><marquee scrollamount="4">{{ description8 }}</marquee></div>
            <div class="col-xs-0 col-md-3" style="background-color: #f5f4f5;text-align: center;">{{ date8 }}</div>
        </div>
    </div>
</div>
</nav>

<script>
var button = document.getElementById('my_button');
var container = document.getElementById('main_container');
button.onclick = function(){
    container.classList.toggle("margin_top");
}
</script>
ttp71kqs

ttp71kqs6#

function changeMargin() {
   var button = document.getElementById("my_button");
   var container = document.getElementById("main_container");

   button.addEventListener("click", function () {
         if (container.style.marginTop == '-80px') {
            container.style.marginTop = "0px";
         } else 
         {
            container.style.marginTop = "-80px";
         }
   });
}
 
window.addEventListener('load', function () {
   changeMargin();
});

相关问题