如何使一个按钮影响HTML中的布尔值?

huangapple go评论76阅读模式
英文:

How to make a button affect a boolean value in html?

问题

I'm still learning html and Javascript, so I'm not too good at it. I'm trying to make it so that when I press a button, some text switches from "On" to "Off" and vice versa.

function switch_status() {

  // Setting status to opposite
  if (status === "Turn Appearance Off:") {
    status = "Turn Appearance On:";
  };
  
  if (status === "Turn Appearance On:") {
    status = "Turn Appearance Off:";
  };
  
  // Printing it out
  status_print.innerHTML = status;
}

var status = "Turn Appearance Off:";
var status_print = document.getElementById("status_print");

// Calling function
switch_status();
<h1 id="status_print">Turn Appearance On:</h1>
<button type="button" onclick="switch_status()">Click Me!</button>

As you can see, it's supposed to switch from "On" to "Off" when I hit the button. But it doesn't seem to work, help would be much appreciated.

英文:

I'm still learning html and Javascript, so I'm not too good at it. I'm trying to make it so that when I press a button, some text switches from "On" to "Off" and vise versa.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function switch_status() {

  //Setting status to opposite
  if (status = &quot;Turn Appearance Off:&quot;) {
    status = &quot;Turn Appearance On:&quot;
  };
  
  if (status = &quot;Turn Appearance On:&quot;) {
    status = &quot;Turn Appearance Off:&quot;
  };
  
  //Printing it out
  status_print.innerHTML = status;
}

var status = &quot;Turn Appearance Off:&quot;;
var status_print = document.getElementById(&quot;status_print&quot;);

//calling function
switch_status();

<!-- language: lang-html -->

&lt;h1 id=&quot;status_print&quot;&gt;Turn Appearance On:&lt;/h1&gt;
&lt;button type=&quot;button&quot; onclick=&quot;switch_status()&quot;&gt;Click Me!&lt;/button&gt;

<!-- end snippet -->

As you can see, it's supposed to switch from On to Off when I hit the button. But it doesn't seem to work, help would much be appreciated.

答案1

得分: 1

A single = 表示赋值,所以当你写:

if (status = "Turn Appearance Off:") {

实际上是将 status 设置为字符串的值。

使用 === 进行严格的相等性检查。

除此之外,查看下面的注释以获取其他需要更改的内容。

英文:

A single = means assignment, so when you write:

  if (status = &quot;Turn Appearance Off:&quot;) {

You are actually setting status to the value of the string.

Use === for strict equality check.

Beyond that, see the comments below for additional things to change.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Use let to define the scope as block level
// Use const to define a constant
let statusMessage = &quot;&quot;;
let status = false;  // Use a Boolean for a binary value
const status_print = document.getElementById(&quot;status_print&quot;);

// Set up click event handler on the button. Do this in JavaScript
// not with HTML event attributes.
document.querySelector(&quot;button&quot;).addEventListener(&quot;click&quot;, switch_status);

function switch_status() {
  // Setting status to opposite
  // Because status is initially false, setting it to the opposite makes it true
  // and vice versa.
  status = !status;

  // Just need to check to see if status === true
  if (status) {
    statusMessage = &quot;Turn Appearance Off:&quot;;
  } else {
    statusMessage = &quot;Turn Appearance On:&quot;;
  }

  // Printing it out
  // Don&#39;t use .innerHTML when there&#39;s no HTML in the string
  status_print.textContent = statusMessage;
}

<!-- language: lang-html -->

&lt;h1 id=&quot;status_print&quot;&gt;Turn Appearance On:&lt;/h1&gt;
&lt;button type=&quot;button&quot;&gt;Click Me!&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 0

这是您代码的轻微修改。el 获取目标元素,用于获取 status 并在完成新值更新后赋予它 newStatus

function switch_status() {
 let el = document.querySelector('#status_print');
 let status = el.innerHTML;
 let newStatus;
  //将状态设置为相反
  if (status === "Turn Appearance Off:") {
    newStatus = "Turn Appearance On:"
  };
  
  if (status === "Turn Appearance On:") {
    newStatus = "Turn Appearance Off:"
  };
  
  //将其打印出来
  el.innerHTML = newStatus;
}
<h1 id="status_print">Turn Appearance On:</h1>
<button type="button" onclick="switch_status()">Click Me!</button>
英文:

Here's a slight adaptation of your code. el gets the target element, and is used to get the status and also to give it the newStatus once you're finished updating the new value.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function switch_status() {
 let el = document.querySelector(&#39;#status_print&#39;);
 let status = el.innerHTML;
 let newStatus;
  //Setting status to opposite
  if (status === &quot;Turn Appearance Off:&quot;) {
    newStatus = &quot;Turn Appearance On:&quot;
  };
  
  if (status === &quot;Turn Appearance On:&quot;) {
    newStatus = &quot;Turn Appearance Off:&quot;
  };
  
  //Printing it out
  el.innerHTML = newStatus;
}

<!-- language: lang-html -->

&lt;h1 id=&quot;status_print&quot;&gt;Turn Appearance On:&lt;/h1&gt;
&lt;button type=&quot;button&quot; onclick=&quot;switch_status()&quot;&gt;Click Me!&lt;/button&gt;

<!-- end snippet -->

答案3

得分: 0

Sure, here are the translated code parts:

if (status = "关闭外观:")

and

if (status = "打开外观:")

都执行将status赋值为真值字符串的条件,这意味着条件将始终为真,每个块都会依次执行。

这意味着

    status = "关闭外观:"

将始终在更新<h1>元素之前执行。

您必须使用=====来进行相等比较。

请注意,这可以使用单个布尔值进行控制,而无需进行字符串比较。bool = !bool是快速将布尔值从true翻转为false,反之亦然的方法。

这可以简化为:

const display = document.getElementById("status_print");
let isOn = false;

document.getElementById("toggle").addEventListener("click", () => {
  isOn = !isOn;
  
  display.innerHTML = `关闭外观:${isOn ? "打开" : "关闭"}:`;
});
<h1 id="status_print">打开外观:</h1>
<button type="button" id="toggle">点击我!</button>
英文:
if (status = &quot;Turn Appearance Off:&quot;)

and

if (status = &quot;Turn Appearance On:&quot;) 

are both performing assignment of status to a truthy string value, meaning the conditions will always be true and each block is always executed in turn.

This means

    status = &quot;Turn Appearance Off:&quot;

will always be executed just before the &lt;h1&gt; element is updated.

You must use == or === for comparing equality.

Note that this could be controlled with a single boolean, instead of doing string comparisons. bool = !bool is a quick way to flip a boolean value from true to false, and vice versa.

This can be reduced to:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const display = document.getElementById(&quot;status_print&quot;);
let isOn = false;

document.getElementById(&quot;toggle&quot;).addEventListener(&quot;click&quot;, () =&gt; {
  isOn = !isOn;
  
  display.innerHTML = `Turn Appearance ${isOn ? &quot;Off&quot; : &quot;On&quot;}:`;
});

<!-- language: lang-html -->

&lt;h1 id=&quot;status_print&quot;&gt;Turn Appearance On:&lt;/h1&gt;
&lt;button type=&quot;button&quot; id=&quot;toggle&quot;&gt;Click Me!&lt;/button&gt;

<!-- end snippet -->

答案4

得分: 0

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
const element = document.getElementById("status_print");
var is_shown = false;

function switch_status() {
  //Setting status to opposite
  element.innerHTML = is_shown ? "Turn Appearance On:" : "Turn Appearance Off:";
  is_shown = !is_shown;
}

<!-- language: lang-html -->
<h1 id="status_print">Turn Appearance On:</h1>
<button onclick="switch_status()">Click Me!</button>

<!-- end snippet -->

只返回翻译好的代码部分。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  const element = document.getElementById(&quot;status_print&quot;);
  var is_shown = false;

  function switch_status() {
    //Setting status to opposite
    element.innerHTML = is_shown ? &quot;Turn Appearance On:&quot; : &quot;Turn Appearance Off:&quot;
    is_shown = !is_shown;
  }

<!-- language: lang-html -->

&lt;h1 id=&quot;status_print&quot;&gt;Turn Appearance On:&lt;/h1&gt;
&lt;button onclick=&quot;switch_status()&quot;&gt;Click Me!&lt;/button&gt;

<!-- end snippet -->

Just for a good practice - you should use a boolean to toggle the statement, not set a string and check if it is exactly that string. Even tho this is also working, it's not really good practice 如何使一个按钮影响HTML中的布尔值?

In my code snippet, the element and a boolean gets declared globally and inside the switch function, the innerHTML of the element is getting set either to the one or the other string, depending on the boolean. After that, the boolean is toggled.

huangapple
  • 本文由 发表于 2023年7月18日 06:32:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定