简体   繁体   English

单击时更改另一个元素的背景颜色

[英]Changing the background colour of another element on click

I would like to add accessibility options to a website to give the user the chance to change the background of the following element (not the whole document background):我想向网站添加可访问性选项,让用户有机会更改以下元素的背景(而不是整个文档背景):

   .ast-separate-container .ast-article-single {
    background-color: #fffff0;
    }

For example, I would like to display coloured boxes or text for: Pink White Blue Yellow and when the links are clicked the background colour changes.例如,我想显示彩色框或文本:粉红色白色蓝色黄色,当点击链接时,背景颜色会发生变化。

Thanks in advance for any advice.提前感谢您的任何建议。

In this situation you should use JS and add event listener to this component:在这种情况下,您应该使用 JS 并向该组件添加事件侦听器:

element.addEventListener('click', function() {
  element.classList.add(/* class with corresponding styles */)
});

Have a look at this code snippet, which uses javscript to achieve that:看看这个代码片段,它使用 javscript 来实现:

 var background = document.getElementById('background'); function setBackgroundTo(color) { background.style.backgroundColor = color; }
 #background { height: 100px; width: 100px; background-color: red; }
 The div below simulates your background. Click a button to change its color. <div id="background"></div> <button onclick="setBackgroundTo('red')">Red</button> <button onclick="setBackgroundTo('blue')">Blue</button> <button onclick="setBackgroundTo('green')">Green</button> <button onclick="setBackgroundTo('#000')">Black</button>

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

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