如何使用javascript通过按钮onclick事件更改div HTML标记

时间:2017-11-06 09:52:59

标签: javascript html

<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>

我有一个带按钮id="my_button"的简单标题HTML div结构。在按钮事件上点击第一个带有id="main_container"的div标签应更改css指令,特别是从-80px0px的margin-top值以及margin-top值等于0px时应更改为-80px

我是网络开发的新手。我想我需要一个带有OnClick事件处理程序的javascript脚本函数。

5 个答案:

答案 0 :(得分:2)

使用以下函数更改margin-top,使用javascript

<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>

答案 1 :(得分:0)

点击按钮调用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

答案 2 :(得分:0)

- 您应该使用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>

答案 3 :(得分:0)

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

button.addEventListener("click", function () {
  container.style.marginTop = "0px"
})
&#13;
<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>
&#13;
&#13;
&#13;

但我建议使用transform以获得更好的性能。例如:

之前:transform: translateY(-80px)。 之后:transform: translateY(0)

答案 4 :(得分:0)

请尝试以下代码,我删除了您的内联样式并添加了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>