简体   繁体   English

按钮不会消失onClick(JavaScript)

[英]button doesn't dissapear onClick (JavaScript)

I have the following code for making the button not visible and it works for a second and then button comes again. 我有以下代码使按钮不可见,它工作一秒钟,然后再次按钮。 The links on navigates on the same page I have tried "return false;" 在我试过的同一页面上导航的链接“返回false;” but then my navigation doesn't work. 但后来我的导航无效。

What to do for keeping the button hidden? 如何保持隐藏按钮?

JavaScript JavaScript的

function btn_hide(){

    document.getElementById("btn_shfaqe").style.display="none";
}

html HTML

<a href="?tip=fin&vid_id=0" onClick="btn_hide();">test1</a>

You have to do two things; 你必须做两件事; Return the function and return false, like this: 返回函数并返回false,如下所示:

javascript JavaScript的

function btn_hide(){

    document.getElementById("btn_shfaqe").style.display="none";
    return false;
}

html HTML

<a href="?tip=fin&vid_id=0" onClick="return btn_hide();">test1</a>

Here's a DEMO 这是一个演示

EDIT according to comment 根据评论编辑
You are better off hiding the button serverside, but if you really want to use javascript you can do this on page load: 你最好隐藏服务器按钮,但如果你真的想使用javascript,你可以在页面加载时执行此操作:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

window.onload = function() {
    var vid_id = getParameterByName('vid_id');
    if (vid_id == 0 || vid_id == 5) {
        document.getElementById("btn_shfaqe").style.display="none";
    }
}

It is an anchor tag . 它是一个锚标签。 I will navigate you to another page . 我将导航到另一页。

If you don't want to navigate to another page you may use 如果您不想导航到您可能使用的其他页面

javascript:void(0) 的javascript:无效(0)

as

<a href="javascript:void(0)" onclick="btn_hide();">

Try this fot javascript : 试试这个照片javascript:

<script>
    function visibilite() {
        var targetElement = document.getElementById(\'div_connexion\');
        targetElement.style.display = "none";
    }
</script>

and this in the html : 这在html中:

<div id="div_connexion"><a class="connexion" href="javascript:visibilite();">Connexion</a></div>

I have this on my website and when I click on the div it desapear until the user refresh the page. 我在我的网站上有这个,当我点击div时它会消失,直到用户刷新页面。

Please try with thw below code snippet. 请尝试下面的代码片段。

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
            return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }

        function btn_hide() {
            document.getElementById("btn_shfaqe").style.display = "none";
        }
    </script>
</head>
<body>
        <div>
        <button id="btn_shfaqe" style="display: none">
            jayesh</button>
        <a href="?tip=fin&vid_id=0" onclick="btn_hide();">test1</a>
        <script type="text/javascript">
            if (getParameterByName("tip") == "") {
                document.getElementById("btn_shfaqe").style.display = "";
            }
        </script>
    </div>
</body>
</html>

The links on navigates on the same page I have tried "return false;" 在我试过的同一页面上导航的链接“返回false;” but then my navigation doesn't work. 但后来我的导航无效。

You want to hide the link and still be able to navigate? 您想要隐藏链接仍然可以导航?

There are two ways of solving the problem: 有两种方法可以解决这个问题:

  1. Server-side: add a parameter to your url, like so: ?tip=fin&vid_id=0&hideButton=1 and on server side apply the display: none; 服务器端:在您的网址中添加一个参数,如下所示: ?tip=fin&vid_id=0&hideButton=1 ,在服务器端应用display: none; style to your element if it is set. 如果已设置,则为您的元素设置样式。 If you're using PHP, something along the lines of the following should do the trick: 如果你正在使用PHP,那么下面的内容应该可以解决问题:

    <?php if (isset($_GET['hideButton'])) { echo 'style="display: none;"'; }

  2. Client-side: write some flag value to localStorage when button is clicked. 客户端:单击按钮时向localStorage写入一些标志值。 When the page is loaded, check if the flag is set. 加载页面时,检查是否设置了标志。 If it is set - hide the anchor. 如果已设置 - 隐藏锚点。

     // Onclick handler: myButton.addEventListener('click', function () { localStorage.setItem('hideButton', 'yes'); }, false); // Onload handler: window.addEventListener('load', function () { if (localStorage.getItem('hideButton') === 'yes') { myButton.style.display = 'none'; } }); 

Using one of these ways will hide the link while keeping navigation working. 使用这些方法之一将隐藏链接,同时保持导航工作。 You don't even need to hide the button in the onclick event handler. 您甚至不需要在onclick事件处理程序中隐藏按钮。

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

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