簡體   English   中英

單擊按鈕后如何隱藏div?

[英]How to hide div after clicking on button?

當用戶在同一頁面導航時點擊按鈕,我想隱藏 div(即“隱藏 div”)。 請幫我解決這個問題。 謝謝

<div id="hide-div">
   <button type="button" title="test" class="button btn-small" onclick="setLocation('abc.com');"><span>'Add to Cart'</span></button>
</div>

使用原生JavaScript 雖然我建議使用事件處理程序而不是內聯 JS,但這是考慮到您的代碼。

  1. 使用style.display隱藏元素
  2. 您可以在使用window.location隱藏按鈕后重定向頁面

 function setLocation(loc) { document.getElementById('hide-div').style.display = 'none'; window.location = loc; }
 <div id="hide-div"> <button type="button" title="test" class="button btn-small" onclick="setLocation('http://google.com')"><span>'Add to Cart'</span></button> </div>

你可以通過簡單的 jquery 做到這一點

 <script> jQuery(document).ready(function($) { $(".button").click(function(){ $("div").fadeOut(); }); }); </script>

使用 jQuery,您可以在 onclick 中執行此操作:

$('#hide-div').hide()

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

使用 jQuery 隱藏它:

$('#hide-div button').click(function(){
    $(this).parent().hide();
});

我假設您已經將 jQuery 添加到您的頁面中。 所以,可能是這樣的:

 $('#hide-div .button').on('click', function() { $('#hide-div').hide(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="hide-div"> <button type="button" title="test" class="button btn-small" onclick="setLocation('abc.com');"><span>'Add to Cart'</span></button> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM