如何仅使用指定链接的javascript隐藏网页中的元素?

时间:2018-07-27 13:55:03

标签: javascript jquery html

我正在尝试使用以下脚本在dynamic page中隐藏按钮

  <a class="hideonClick" id="venueButton" type="button">Find venue providers</a>
<script>
document.getElementById("venueButton").onclick = function() {hideButton()};

function hideButton(){
    //get the button

                venueButton.href="http://evopia.net/?providers-category=venues&catid=123&pagenum=1&viewtype=grid-4&numberofpages=&setorderby=&setorder=";
                venueButton.style.display="none";       
    }
    window.onload = init;
</script>
</pre>

问题是,我正在使用“ href”重定向到给定的链接,我只想在使用的“ href”链接上隐藏此按钮。请帮助使用哪种方法。

2 个答案:

答案 0 :(得分:0)

您的问题有点令人困惑:

  

我正在使用“ href”重定向到给定的链接,我只想在使用的“ href”链接上隐藏此按钮

在我看来,您只想单击时隐藏链接。这就是我要使用的方法:

document.getElementById("venueButton").addEventListener('click', hideButton);

function hideButton(e) {
  //this.href = "put your url here";
  console.log('commented out setting href for example');
  this.style.display = "none";
}
<a class="hideonClick" id="venueButton" type="button">Find venue providers</a>

如果您只是试图将数据加载到视图中,则应使用AJAX request using XMLHttpRequest来获取数据,而不要通过将href指向其自身的url来静态地重新加载页面。

答案 1 :(得分:-1)

我个人会在点击时添加一个cookie,如果是cookie,则在hide上添加一个button

How do I set/unset a cookie with jQuery?