简体   繁体   English

单击窗口/另一个元素时如何更改所有元素的颜色?

[英]How to change the color of all elements when the window/another element is clicked?

I am creating a webpage with a list of all the content headings at the top.我正在创建一个网页,顶部列出了所有内容标题。 When the user clicks of each of these, the related part of the webpage should turn blue.当用户点击其中的每一个时,网页的相关部分应变为蓝色。 However, it does not.但是,事实并非如此。 There must be something wrong with my javascript.我的 javascript 一定有问题。 The initial list:初始名单:

<h1><u>Quotations!</u></h1>
<ol>Index:
  <li id="bq" onclick="bq()">blockquotes</li>
  <li id="sq" onclick="sq()">Short Quotations</li>
  <li id="a" onclick="a()">Abbreviations</li>
  <li id="ad" onclick="ad()">Addresses</li>
  <li id="c" onclick="c()">Cites</li>
  <li id="bdo" onclick="bdo()">BDOs</li>
</ol>

The contents of the page:页面内容:

<div id="bqdiv">
  <h5>Blockquotes</h5>
  <blockquote>
    These have indents! They define a section that is quoted from another source.
  </blockquote>
</div>
<div id="sqdiv">
  <h5>Short Quotations (q tag)</h5>
  <p>This is an example: 
    <q>Hello</q>
  </p>
</div>
<div id="a">
  <h5>Abbreviations</h5> 
  <p>These can be <abbr title= "HEYA!">hovered</abbr> over!</p>
</div>
<div id="addiv">
  <h5>Addresses</h5>
  This is an address:
  <address>
    Addresses are in italic <br>
    and browsers always add a break b4 and after the <address></address> element
  </address>
  After the address
</div>
<div id="cdiv">
  <h5>Cites</h5>
  <p>
    <cite>Cites</cite> define the title of something creative, like a painting!<br>
    They are in italic, like this.
  </p>
</div>
<div id="bdodiv">
  <h5>Bi-Directional Override (BDO)</h5>
  <p>It is used to override the text direction, <bdo dir="rtl">like this!</bdo></p>
</div>

The javascript I have so far:到目前为止,我拥有的 javascript:

function bq() {
  document.getElementById("bqdiv").style.color="blue";
}
function sq() {
  document.getElementById("sqdiv").style.color="blue"'
}
function ad() {
  document.getElementById("addiv").style.color="blue";
}
function c() {
  document.getElementById("cdiv").style.color="blue";
}
function bdo() {
  document.getElementById("bdodiv").style.color="blue";
}
window.addEventListener("click", function(event)) {
                document.getElementById("bqdiv").style.color="black"
                document.getElementById("sqdiv").style.color="black"
                document.getElementById("addiv").style.color="black"
                document.getElementById("cdiv").style.color="black"
                document.getElementById("bdodiv").style.color="black"
            }

Looks like you're doing function {} instead of function(){} when you call window.onClick.当您调用 window.onClick 时,看起来您正在执行function {}而不是function(){} And you should try to use this instead if possible如果可能的话,你应该尝试使用它

window.addEventListener("click", function(event) {
});

How about making a "black" function instead of window.onclick and call it before setting color to blue?如何制作一个“黑色” function 而不是window.onclick并在将颜色设置为蓝色之前调用它?

function black() {
  document.getElementById("bqdiv").style.color="black";
  document.getElementById("sqdiv").style.color="black";
  document.getElementById("addiv").style.color="black";
  document.getElementById("cdiv").style.color="black";
  document.getElementById("bdodiv").style.color="black";
}

And in each div's onclick function call it (example below on one of them):并在每个 div 的 onclick function 中调用它(以下示例之一):

function bq() {
  black();
  document.getElementById("bqdiv").style.color="blue";
}

Oh, I just realized you want anywhere else clicked to blacken the divs, so keep window.onclick - just make it call black() .哦,我刚刚意识到你想在其他任何地方点击以使 div 变黑,所以保留window.onclick - 只需让它调用black()

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

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