简体   繁体   English

页面加载时如何将 javascript 按钮切换为关闭?

[英]How to toggle javascript buttons as off when page loads?

I'm trying to create a website that uses a hint button and a solution button to each of the problems it provides.我正在尝试创建一个网站,该网站对它提供的每个问题都使用提示按钮和解决方案按钮。 I'm using a javascript function and a button that shows a new div when clicked, but it automatically shows the hint and solution as the page loads.我正在使用 javascript function 和一个在单击时显示新 div 的按钮,但它会在页面加载时自动显示提示和解决方案。

Is there a way I can set it so all buttons are automatically hidden?有没有办法可以设置它让所有按钮都自动隐藏? Or maybe can I get the function to run once as the page loads, I think that would work.或者也许我可以让 function 在页面加载时运行一次,我认为这会起作用。 Any ideas?有任何想法吗?

<button onclick="hint0()">Hint</button>
<div id="hint0">
  Try reading the man pages for some of the listed commands on the OverTheWire level page. They may be useful!
</div>
<script>
  function hint0() {
    var x = document.getElementById("hint0");
    if (x.style.display === "none") {
      x.style.display = "block";
    } else {
      x.style.display = "none";
    }
  }
</script>  

Without seeing your code, it's a little difficult to give any feedback, but I can suggest a few things.没有看到你的代码,很难给出任何反馈,但我可以提出一些建议。 You can use css to hide your hint and solutions - setting the display to none would be the easiest.您可以使用 css 隐藏您的提示和解决方案 - 将显示设置为无将是最简单的。 The hints and solutions would be visible to anyone who viewed the page source or inspected the elements, if you only hide them.如果您只隐藏提示和解决方案,任何查看页面源或检查元素的人都可以看到提示和解决方案。 You could write an empty hint and solution container into your markup and then add the hint and solution text when the user interacts with the button.您可以在标记中写入一个空的提示和解决方案容器,然后在用户与按钮交互时添加提示和解决方案文本。 With Javascript or jQuery, on your click event, append the hint or solution to the DOM or set the innerHTML of the element.使用 Javascript 或 jQuery,在您的点击事件中,append 提示或解决 DOM 或设置元素的 innerHTML。

Ok, I see your example code now - you can set a function to run on load and select your element by id and set it to display none.好的,我现在看到您的示例代码 - 您可以设置 function 以在加载时运行,并通过 id 设置 select 您的元素并将其设置为不显示。 This page can help you understand the load event - https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event此页面可以帮助您了解加载事件 - https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event

Use CSS in the header (not as a <link> , but actually inlined) to force those components hidden, then use JavaScript to show them later.使用 header 中的 CSS (不是作为<link> ,而是实际内联)强制隐藏这些组件,然后使用 JavaScript 稍后显示它们。

example:例子:

.my-secret-div {
  display:none;
}

... ...

<div class="my-secret-div"> the hint goes here </div>

... ...

Then use Javascript to remove the my-secret-div class.然后使用 Javascript 删除my-secret-div class。

Note that this doesn't hide the content from someone who could download the page or "inspect" the content using the browser developer tools.请注意,这不会向可以下载页面或使用浏览器开发人员工具“检查”内容的人隐藏内容。

Example fiddle: https://jsfiddle.net/5qnoghrf/小提琴示例: https://jsfiddle.net/5qnoghrf/

I think you mean to hide content of div not the buttons, just set your items to: style="display:none" inline.我认为您的意思是隐藏 div 的内容而不是按钮,只需将您的项目设置为: style="display:none" inline。

 function solution0() { var x = document.getElementById("solution0"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } function hint0() { var x = document.getElementById("hint0"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } }
 <button onclick="hint0()">Hint</button> <div id="hint0" style="display:none"> Try reading the man pages for some of the listed commands on the OverTheWire level page. They may be useful: </div> <button onclick="solution0()">Solution</button> <div id="solution0" style="display.none"> You need to use cat to print the contents of the file "readme". The file contains the password that you can copy and paste to log in to bandit1. </div>

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

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