按钮文本颜色在我将脚本放在另一个函数内时未更改。

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

button text color not changing when I place the script inside another function

问题

function correct(x) {
  document.getElementById("x").style.color = "绿色";
}

function wrong(y) {
  document.getElementById("y").style.color = "红色";
}

function answer() {
  if (document.getElementById("p").innerHTML == "问题 1") {
    correct(bt1);
    wrong(bt2);
  } else if (document.getElementById("p").innerHTML == "问题 2") {
    correct(bt2);
    wrong(bt1);
  }
}
<p id="p">问题 1</p>

<button id="bt1" onclick="answer()">选项 1</button>
<button id="bt2" onclick="answer()">选项 2</button>
英文:

I want to change the text color of a button when I click on it using a function.
so I made a function for it which does not work.

code:

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

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

function correct(x) {
  document.getElementById(&quot;x&quot;).style.color = &quot;green&quot;;
}

function wrong(y) {
  document.getElementById(&quot;y&quot;).style.color = &quot;red&quot;;
}

function answer() {
  if (document.getElementById(&quot;p&quot;).innerHTML == &quot;question 1&quot;) {
    correct(bt1);
    wrong(bt2);
  } else if (document.getElementById(&quot;p&quot;).innerHTML == &quot;question 2&quot;) {
    correct(bt2);
    wrong(bt1);
  }
}

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

&lt;p id=&quot;p&quot;&gt;question 1&lt;/p&gt;

&lt;button id=&quot;bt1&quot; onclick=&quot;answer()&quot;&gt; option 1&lt;/button&gt;
&lt;button id=&quot;bt2&quot; onclick=&quot;answer()&quot;&gt; option 2&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

你的引号放反了:correctwrong 函数的参数应该被引号包围;在这些函数中使用它们的参数则不应该被引号包围。

英文:

You have your quotes backwards: the arguments to correct and wrong should be quoted; the use of their parameters in those functions should not.

答案2

得分: 1

我已经直接在你的代码中添加了注释。看起来你可能在错误的地方放置了字符串标记。

<script>
  function correct(x) {
    // 你正在寻找一个名为"x"的字符串,而不是由x表示的id
    document.getElementById(x).style.color = "green";
  }

  function wrong(y) {
    // 同样的问题
    document.getElementById(y).style.color = "red";
  }

  function answer() {
    if (document.getElementById("p").innerHTML == "question 1") {
      // correct("bt1");
      correct(bt1);
      // wrong("bt2");
      wrong(bt2);
    } else if (document.getElementById("p").innerHTML == "question 2") {
      // correct("bt2");
      correct(bt2);
      // wrong("bt1");
      wrong(bt1);
    }
  }
</script>

希望这可以帮助你理解代码中的问题。

英文:

I have added comments to your code directly. It seems that you may have put string markers at the wrong place

   &lt;script&gt;
    function correct(x) {
      // you are looking for a string &quot;x&quot; instead of the id represented by x
      document.getElementById(&quot;x&quot;).style.color = &quot;green&quot;;
    }
    
    function wrong(y) {
      // same problem
      document.getElementById(&quot;y&quot;).style.color = &quot;red&quot;;
    }
    
    function answer() {
      if (document.getElementById(&quot;p&quot;).innerHTML == &quot;question 1&quot;) {
        // correct(&quot;bt1&quot;);
        correct(bt1);
        // wrong(&quot;bt2&quot;);
        wrong(bt2);
      } else if (document.getElementById(&quot;p&quot;).innerHTML == &quot;question 2&quot;) {
        // correct(&quot;bt2&quot;);
        correct(bt2);
        // wrong(&quot;bt1&quot;);
        wrong(bt1);
      }
    }
    &lt;/script&gt;

答案3

得分: 1

你不需要调用 getElementById() 来获取按钮的更改。answer() 直接传递了按钮,使用这些参数。

function correct(x) {
  x.style.color = "green";
}

function wrong(y) {
  y.style.color = "red";
}

function answer() {
  if (document.getElementById("p").innerHTML == "question 1") {
    correct(bt1);
    wrong(bt2);
  } else if (document.getElementById("p").innerHTML == "question 2") {
    correct(bt2);
    wrong(bt1);
  }
}
<p id="p">question 1</p>
<button id="bt1" onclick="answer()"> option 1</button>
<button id="bt2" onclick="answer()"> option 2</button>

请注意,使用 bt1bt2 作为参数利用了元素ID自动成为全局变量的特性。一般来说,这不被认为是良好的编程风格。

英文:

You don't need to call getElementById() to get the buttons to change. answer() passes the buttons directly, use the parameters.

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

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

function correct(x) {
  x.style.color = &quot;green&quot;;
}

function wrong(y) {
  y.style.color = &quot;red&quot;;
}

function answer() {
  if (document.getElementById(&quot;p&quot;).innerHTML == &quot;question 1&quot;) {
    correct(bt1);
    wrong(bt2);
  } else if (document.getElementById(&quot;p&quot;).innerHTML == &quot;question 2&quot;) {
    correct(bt2);
    wrong(bt1);
  }
}

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

&lt;p id=&quot;p&quot;&gt;question 1&lt;/p&gt;

&lt;button id=&quot;bt1&quot; onclick=&quot;answer()&quot;&gt; option 1&lt;/button&gt;
&lt;button id=&quot;bt2&quot; onclick=&quot;answer()&quot;&gt; option 2&lt;/button&gt;

<!-- end snippet -->

Note that use bt1 and bt2 as arguments takes advantage of the fact that element IDs automatically become global variables. This is not generally considered good style.

答案4

得分: 1

以下是翻译好的部分:

由于您将按钮传递给函数但未使用它,只需将其更改为以下内容:

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

<!-- language: lang-js -->
    
    function correct(element) {
      element.style.color = "green";
    }

    function wrong(element) {
      element.style.color = "red";
    }

    function answer() {
      if (document.getElementById("p").textContent === "question 1") {
        correct(bt1);
        wrong(bt2);
      } else if (document.getElementById("p").textContent === "question 2") {
        correct(bt2);
        wrong(bt1);
      }
    }

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

    <p id="p">question 1</p>

    <button id="bt1" onclick="answer()"> option 1</button>
    <button id="bt2" onclick="answer()"> option 2</button>

<!-- end snippet -->

更好的方法是使用类而不是ID。

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

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

    function correct(element) {
      element.style.color = "green";
    }

    function wrong(element) {
      element.style.color = "red";
    }

    function answer(event) {
      const question = event.target.closest('.question');
      const prompt = question.querySelector('.prompt');
      if (prompt.textContent === "Question 1") {
        correct(question.querySelector('.btn:nth-of-type(1)'));
        wrong(question.querySelector('.btn:nth-of-type(2)'));
      } else if (prompt.textContent === "Question 2") {
        wrong(question.querySelector('.btn:nth-of-type(1)'));
        correct(question.querySelector('.btn:nth-of-type(2)'));
      }
    }

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

    <div class="question">
      <p class="prompt">Question 1</p>
      <button class="btn" onclick="answer(event)">Option 1</button>
      <button class="btn" onclick="answer(event)">Option 2</button>
    </div>

    <div class="question">
      <p class="prompt">Question 2</p>
      <button class="btn" onclick="answer(event)">Option 1</button>
      <button class="btn" onclick="answer(event)">Option 2</button>
    </div>

<!-- end snippet -->
英文:

Since you are passing the button into the function but not using it, just change it to the following:

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

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

function correct(element) {
element.style.color = &quot;green&quot;;
}
function wrong(element) {
element.style.color = &quot;red&quot;;
}
function answer() {
if (document.getElementById(&quot;p&quot;).textContent === &quot;question 1&quot;) {
correct(bt1);
wrong(bt2);
} else if (document.getElementById(&quot;p&quot;).textContent === &quot;question 2&quot;) {
correct(bt2);
wrong(bt1);
}
}

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

&lt;p id=&quot;p&quot;&gt;question 1&lt;/p&gt;

&lt;button id=&quot;bt1&quot; onclick=&quot;answer()&quot;&gt; option 1&lt;/button&gt;
&lt;button id=&quot;bt2&quot; onclick=&quot;answer()&quot;&gt; option 2&lt;/button&gt;

<!-- end snippet -->

A better approach would be to use classes instead of IDs.

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

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

function correct(element) {
element.style.color = &quot;green&quot;;
}
function wrong(element) {
element.style.color = &quot;red&quot;;
}
function answer(event) {
const question = event.target.closest(&#39;.question&#39;);
const prompt = question.querySelector(&#39;.prompt&#39;);
if (prompt.textContent === &quot;Question 1&quot;) {
correct(question.querySelector(&#39;.btn:nth-of-type(1)&#39;));
wrong(question.querySelector(&#39;.btn:nth-of-type(2)&#39;));
} else if (prompt.textContent === &quot;Question 2&quot;) {
wrong(question.querySelector(&#39;.btn:nth-of-type(1)&#39;));
correct(question.querySelector(&#39;.btn:nth-of-type(2)&#39;));
}
}

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

&lt;div class=&quot;question&quot;&gt;
&lt;p class=&quot;prompt&quot;&gt;Question 1&lt;/p&gt;
&lt;button class=&quot;btn&quot; onclick=&quot;answer(event)&quot;&gt;Option 1&lt;/button&gt;
&lt;button class=&quot;btn&quot; onclick=&quot;answer(event)&quot;&gt;Option 2&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;question&quot;&gt;
&lt;p class=&quot;prompt&quot;&gt;Question 2&lt;/p&gt;
&lt;button class=&quot;btn&quot; onclick=&quot;answer(event)&quot;&gt;Option 1&lt;/button&gt;
&lt;button class=&quot;btn&quot; onclick=&quot;answer(event)&quot;&gt;Option 2&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案5

得分: 1

首先,您的correct()wrong()函数尝试获取ID为"x"和"y"的元素,但我认为您想要从函数参数中获取ID。
其次,您将"bt1"和"bt2"作为未定义的变量发送到correct()wrong()函数中,您应该将它们发送为字符串"bt1"和"bt2"。

<p id="p">问题 1</p>

<button id="bt1" onclick="answer()">选项 1</button>
<button id="bt2" onclick="answer()">选项 2</button>

<script>
function correct(id) {
  document.getElementById(id).style.color = "green";
}

function wrong(id) {
  document.getElementById(id).style.color = "red";
}

function answer() {
  if (document.getElementById("p").innerHTML == "问题 1") {
    correct("bt1");
    wrong("bt2");
  } else if (document.getElementById("p").innerHTML == "问题 2") {
    correct("bt2");
    wrong("bt1");
  }
}
</script>
英文:

Firstly, your correct() and wrong() functions are trying to get elements with ID "x" and "y" respectively, but what i think you wanted is obtain the id from the function parameters.
Secondly, you're sending to the correct() and wrong() functions, the IDs of the buttons as bt1 and bt2, but these are undefined, you should send them as strings "bt1", "bt2".

&lt;p id=&quot;p&quot;&gt;question 1&lt;/p&gt;
&lt;button id=&quot;bt1&quot; onclick=&quot;answer()&quot;&gt; option 1&lt;/button&gt;
&lt;button id=&quot;bt2&quot; onclick=&quot;answer()&quot;&gt; option 2&lt;/button&gt;
&lt;script&gt;
function correct(id) {
document.getElementById(id).style.color = &quot;green&quot;;
}
function wrong(id) {
document.getElementById(id).style.color = &quot;red&quot;;
}
function answer() {
if (document.getElementById(&quot;p&quot;).innerHTML == &quot;question 1&quot;) {
correct(&quot;bt1&quot;);
wrong(&quot;bt2&quot;);
} else if (document.getElementById(&quot;p&quot;).innerHTML == &quot;question 2&quot;) {
correct(&quot;bt2&quot;);
wrong(&quot;bt1&quot;);
}
}
&lt;/script&gt;

答案6

得分: 1

其他人已经指出,您的变量和引号的使用是导致问题的原因,但除此之外,您实际上不需要也不应该设置专门的函数来更改颜色。相反,您应该设置定义所需的可能“外观”的CSS类,然后根据需要添加或删除这些类。

此外,按钮实际上不是回答问题的好选择。这是复选框和单选按钮的用途。但是,您可以隐藏复选框或单选按钮,并使用label元素将它们包装起来,显示为按钮的样式。这将为您提供(在单选按钮的情况下)所需的互斥性。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
// 在JavaScript中设置您的事件,而不是在HTML中
// 创建一个事件处理程序,它将通过单击“问题”div中的任何位置触发。
// 单击将冒泡并在那里处理。

document.addEventListener("click", function(event){
  // 检查单击是否源自我们关心处理的元素
  if(event.target.classList.contains("response")){
    // 清除任何先前的类
    event.target.className = "";
    // 检查选择了哪个答案
    if(event.target.textContent === "3.14"){
      event.target.classList.add("green");
    } else {
      event.target.classList.add("red");   
    }
  }
});


<!-- 语言: lang-css -->
/* 隐藏单选按钮(但按钮的标签仍然可见) */
input[type="radio"] { display:none; }

/* 使标签看起来像按钮 */
label { border:1px solid #808080; border-radius:3px; padding:5px; background:#e0e0e0; }
label:active { background:#a0a0a0; }

/* 颜色 */
.red { background:red; }
.green { background:green; }


<!-- 语言: lang-html -->
<div class="question">
  <p>Which number below represents Pi?</p>
  <label class="response"><input type="radio" name="question1">4.13</button></label>
  <label class="response"><input type="radio" name="question1">3.14</button></label>
</div>

<!-- 结束代码片段 -->
英文:

Others have already pointed out that your variables and the use of quotes is what is causing your problem, but beyond that, you really don't need and shouldn't be setting up dedicated functions just to change the color. Instead, you should set up CSS classes that define the possible "looks" you want and then simply add or remove the classes as needed.

Additionally, buttons aren't really a good choice for answers to questions. This is what checkboxes and radio buttons are for. But, you can hide the checkboxes or radio buttons and use label elements to wrap them and show the labels, styled as buttons. This will give you the mutual exclusivity (in the case of radio buttons) that you desire.

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

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

// Set up your events in JavaScript, not HTML
// Create one event handler that will be triggered by a click
// anywhere within the &quot;question&quot; div. That click will bubble up
// and be handled there.
document.addEventListener(&quot;click&quot;, function(event){
// Check to see if the click originated at an element we care to handle
if(event.target.classList.contains(&quot;response&quot;)){
// Clear any previous classes
event.target.className = &quot;&quot;;
// Check to see which answer was chosen
if(event.target.textContent === &quot;3.14&quot;){
event.target.classList.add(&quot;green&quot;);
} else {
event.target.classList.add(&quot;red&quot;);   
}
}
});

<!-- language: lang-css -->

/* Hide the radio buttons (but the labels for the buttons will still be visible  */
input[type=&quot;radio&quot;] { display:none; }
/* Make the labels look like buttons */
label { border:1px solid #808080; border-radius:3px; padding:5px; background:#e0e0e0; }
label:active { background:#a0a0a0; }
/* Colors */
.red { background:red; }
.green { background:green; }

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

&lt;div class=&quot;question&quot;&gt;
&lt;p&gt;Which number below represents Pi?&lt;/p&gt;
&lt;label class=&quot;response&quot;&gt;&lt;input type=&quot;radio&quot; name=&quot;question1&quot;&gt;4.13&lt;/button&gt;&lt;/label&gt;
&lt;label  class=&quot;response&quot;&gt;&lt;input type=&quot;radio&quot; name=&quot;question1&quot;&gt;3.14&lt;/button&gt;&lt;/label&gt;
&lt;/div&gt;

<!-- end snippet -->

答案7

得分: 1

请尝试以下代码

HTML

<p id="p">问题 1</p>

<button id="bt1" onclick="answer(this)">选项 1</button>
<button id="bt2" onclick="answer(this)">选项 2</button>

JavaScript

function answer(button) {
  if (document.getElementById("p").innerHTML === "问题 1") {
    if (button.id === "bt1") {
      button.style.color = "green";
    } else {
      button.style.color = "red";
    }
  } else if (document.getElementById("p").innerHTML === "问题 2") {
    if (button.id === "bt2") {
      button.style.color = "green";
    } else {
      button.style.color = "red";
    }
  }
}

要使其正常工作,您应将JavaScript代码放在<head>部分或直接放在闭合的</body>标签之前。

英文:

Try out this code

HTML

&lt;p id=&quot;p&quot;&gt;question 1&lt;/p&gt;
&lt;button id=&quot;bt1&quot; onclick=&quot;answer(this)&quot;&gt; option 1&lt;/button&gt;
&lt;button id=&quot;bt2&quot; onclick=&quot;answer(this)&quot;&gt; option 2&lt;/button&gt;

JavaScript

function answer(button) {
if (document.getElementById(&quot;p&quot;).innerHTML === &quot;question 1&quot;) {
if (button.id === &quot;bt1&quot;) {
button.style.color = &quot;green&quot;;
} else {
button.style.color = &quot;red&quot;;
}
} else if (document.getElementById(&quot;p&quot;).innerHTML === &quot;question 2&quot;) {
if (button.id === &quot;bt2&quot;) {
button.style.color = &quot;green&quot;;
} else {
button.style.color = &quot;red&quot;;
}
}
}

You should place the JavaScript code either in the <head> section or just before the closing </body> tag for it to work properly.

huangapple
  • 本文由 发表于 2023年7月11日 01:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655984.html
匿名

发表评论

匿名网友

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

确定