html - 如何在用户单击时设置对象样式

标签 html css

在链接的 CSS 中,我可以设置用户点击前和用户点击后的样式。但是,当用户单击该对象然后更改其颜色时,我如何才能对简单文本或对象执行此操作。 例如

<style>
.object:onmouseclick {
background-color:green;
padding:5px; 
}
</style>

我们必须写什么来代替 onmouseclick

最佳答案

如果您要为链接设置样式,您将使用 :active:focus 但是当您在 dom 元素上使用它时,您将不得不使用 jQuery 来向单击的元素添加一个类并通过该类应用样式...

$('.object').click(function() {
  $(this).toggleClass('clicked');
});
.object {
  background-color: green;
  padding: 5px;
  cursor: pointer;
  width: 300px;
  color: orange;
}
.object.clicked {
  background-color: blue;
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="object">
  <h1>The Mutants Are Revolting</h1>
  <p>Bender?! You stole the atom. That's the ONLY thing about being a slave. I am Singing Wind, Chief of the Martians. Anyhoo, your net-suits will allow you to experience Fry's worm infested bowels as if you were actually wriggling through them.</p>

</div>

关于html - 如何在用户单击时设置对象样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194503/

相关文章:

javascript - 与 setInterval 结合使用时,img 的 onclick 属性出现问题

css - 我可以根据基于百分比的宽度设置 div 的高度吗?

css - iPhone 4、iPhone 4s 和 iPhone 5 的媒体查询

html - 下拉菜单在 Safari 浏览器中无法正确显示

javascript - 从 UIWebView *不*修改应用程序源启动移动 Safari?

javascript - HTML 表格 - 为数千个单元格设置样式

javascript - 对齐三个按钮,使它们位于两个 Bootstrap Jumbotron 容器的底部

html - 为什么 flex 元素不缩小过去的内容大小?

javascript - 制作从 html 到 ifttt 的触发器

python - 如何使用 lxml 访问评论