HTML和Javascript更改可见性

时间:2016-05-19 02:10:08

标签: javascript html

我尝试过多次搜索。我已经接近了,但还没想出来。我想要做的是当用户达到一定的点击量时,会弹出一个按钮。

<!DOCTYPE html>
<html>
<head>
  <title>++ Increment</title>
</head>
<body>
  <h1>Click the Button</h1>
  <input type="button" id="countButton" onclick="hi()" value="Click Me!"/>
  <p class="ClickCount">Clicks: <a id="amount">0</a></p>
  <input type="button" id="countButton" onclick="store()" value="Store"/>
  <input type="button" style="visibility:hidden;" value="Button" id="Sir" />
</body>
</html>

这是javascript:

var count = 0;
function hi() {
  count += 1;
  document.getElementById("amount").innerHTML = count;
}
if (count >= 20) {
  document.getElementById("Sir").style.visibility = "visible";
}

我想我需要在 hi()函数之外分配 count 变量,但我不知道该怎么做(或者如果那样&# 39;可能)

6 个答案:

答案 0 :(得分:1)

来自手机,请原谅我的伪代码

您是否正在使用jquery,您可以将值存储在a中 $(BTN)。数据( '点击')

否则在hi函数上面定义一个全局变量。

然后你需要将你的逻辑if(点击&gt; 20)放在你的hi函数

答案 1 :(得分:1)

&#13;
&#13;
var count = 0;
function store(){
  alert(count);
}
function hi() {
  count += 1;
  document.getElementById("amount").innerHTML = count;
  if (count >= 4) document.getElementById('Sir').style.display = 'inline-block';
}
&#13;
#Sir{display:none;}
&#13;
<h1>Click the Button</h1>
<input type="button" id="countButton" onclick="hi()" value="Click Me!"/>
<p class="ClickCount">Clicks: <a id="amount">0</a></p>
<input type="button" id="countButton" onclick="store()" value="Store"/>
<input type="button" value="Button" id="Sir" />
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个:

<!DOCTYPE html>
<html>
<head>
  <title>++ Increment</title>
</head>
<body>
  <h1>Click the Button</h1>
  <input type="button" id="countButton" value="Click Me!"/>
  <p class="ClickCount">Clicks: <a id="amount">0</a></p>
  <!-- <input type="button" id="countButton" onclick="store()" value="Store"/> -->
  <input type="button" style="visibility:hidden;" value="Button" id="Sir" />
  <script>
        var count = 0;
        var clickbutton = document.getElementById("countButton");
        var showbutton = document.getElementById("Sir");
        var amountstatus = document.getElementById("amount");

        clickbutton.onclick = function(){
            if(count < 3){
                count++;
                amountstatus.innerHTML = count;
            }
            else{
                showbutton.style.visibility = "visible";
            }
        }
  </script>
</body>
</html>

检查我将此<input type="button" id="countButton" onclick="store()" value="Store"/>放在评论中,这就是为什么你不能有两个同名的id。

答案 3 :(得分:0)

您好,请试试以下代码:

&#13;
&#13;
var count = 0;
function hi() {
  count += 1;
  document.getElementById("amount").innerHTML = count;

  if (count >= 20) {
    document.getElementById("Sir").style.visibility = "visible";
  }
}
&#13;
&#13;
&#13;

只有改变我认为如果条件在hi函数内移动。希望它有效

答案 4 :(得分:0)

我同意扎克的观点。将值存储在数据属性中是执行此操作的最佳方法。这就是我编码的方式。

NSArray *superArray = [super layoutAttributesForElementsInRect:rect];
NSArray *array = [[NSArray alloc] initWithArray:superArray copyItems:YES];
$(function() {
  
  $( "#countButton" ).on( "click", function() {
    
    var clicks = $( this ).data( "clicks" );
    clicks++;
    
    // Display the clicks.
    $( "#amount" ).html( clicks );
    $( this ).data( "clicks", clicks );
    
    // Check to see if the sir button should be displayed. 
    if( clicks >= 20 ) {
      
      $( "#Sir" ).css( "visibility", "visible" );
      
    }
    
  });
  
});

答案 5 :(得分:0)

你的条件在函数hi()之外,因此在加载文件时检查一次,在调用hi()时检查不。

所以你可以把你的情况放在hi()中。

var count = 0;
function hi() {
  count += 1;
  document.getElementById("amount").innerHTML = count;

  if (count >= 20) {
    document.getElementById("Sir").style.visibility = "visible";
  }
}