简体   繁体   English

如何使用 Javascript 将样式绑定到 onclick 元素?

[英]How do I bind style to onclick element using Javascript?

Whenever I click on the left arrow icon, I want the style of the menu to change.每当我单击左箭头图标时,我都希望更改菜单的样式。 Is it possible to bind specific css style when using onclick function?使用 onclick function 时是否可以绑定特定的 css 样式?

i.fas.fa-chevron-circle-left.left

#sidebar-container .menu
  width: 18rem
  transition: 200ms

How I want it to look after onclick function.我希望它如何照顾 onclick function。

#sidebar-container .menu
  width: 10rem
  

Make a class containing the styles you want and you can toggle those on and off using javascript:制作一个包含您想要的 styles 的 class,您可以使用 javascript 打开和关闭它们:

document.getElementById('my-element').classList.toggle('my-class');

This will add the my-class class if the element doesnt have it, and remove the my-class class if the element does have it.如果元素没有它,这将添加my-class class,如果元素有它,则删除my-class class。 You may also use classList.add and classList.remove if you'd like to set it on or off.如果您想打开或关闭它,您也可以使用classList.addclassList.remove

You can easily bind this to a button with inline javascript.您可以轻松地将其绑定到具有内联 javascript 的按钮。 It is recomended to use event listeners but this should do the trick:建议使用事件侦听器,但这应该可以解决问题:

<button onclick="document.getElementById('my-element').classList.toggle('my-class')">Click me to toggle the class</button>

You can change my-elemment to be the ID of the element you want to toggle the class for and my-class to the classname you'd like to use.您可以将my-elemment element 更改为要切换 class 的元素的 ID,并将my-class更改为您想要使用的类名。

It is possible to bind to an element.可以绑定到元素。 You can use document.querySelector() to find that element.您可以使用 document.querySelector() 来查找该元素。

for example:例如:

const el = document.querySelector("i.fas.fa-chevron-circle-left.left")

el.addEventListener("click", function(){
  el.style.transition = "";
}); 

It's almost always easier to just add an overriding class instead of editing single style properties:添加覆盖 class 而不是编辑单一样式属性几乎总是更容易:

el.classList.add("override");

and have that class in css somewhere.并在 css 某处拥有 class 。

.override {
    transition: none !important;
}

You can create a secondary class for styles you want when it is clicked.您可以在单击时为您想要的 styles 创建一个辅助 class。 You can toggle the class like this您可以像这样切换 class

 const menu = document.querySelector("#sidebar-container.menu"); menu.addEventListener('click', function () { // by adding class name menu.classList.toggle("menu-clicked"); });
 #sidebar-container { width: 200px; height: 100vh; background: #ccc; display: flex; padding-top: 20px; align-items: flex-start; justify-content: center; transition: all ease 200ms; } #sidebar-container.menu { background: #ddd; padding: 20px; display: block; width: 100%; cursor: pointer; } #sidebar-container.menu.menu-clicked { background: green; }
 <div id="sidebar-container"> <div class="menu"> Menu </div> </div>

Hope it helps.希望能帮助到你。 Cheers!干杯!

暂无
暂无

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

相关问题 如何将样式元素添加到div并使用javascript将其删除 - How do I add a style element to a div and remove it using javascript 如何更改“ <input> 动态使用Javascript元素? - How do I change the style of an “<input>” element dynamically using Javascript? 使用JavaScript,如何根据另一个元素的CSS样式更改一个元素的CSS样式? - Using JavaScript, how do I change the CSS style of one element based on the CSS style of another element? 如何更改element.style的样式? Java脚本 - How do I change an element.style's style? Javascript 如何仅使用JavaScript定义按钮元素的onclick属性? - How do I define a button element's onclick attribute using just JavaScript? 如何使用 JavaScript 或 JQuery 在页面加载时更改 Div 元素的样式宽度? - How do I change Style Width of Div Element on Page load using JavaScript or JQuery? 如何使用element.style.backgroundImage =“ url(filePath)”将背景图像添加到javascript? - How do I add a background image to javascript using element.style.backgroundImage = “url(filePath)”? 如何使用 JavaScript 或 ZF590B4FDA2C30BE28DD3C8C3CAF5C7 克隆 HTML 元素的样式 object? - How do I clone an HTML element's style object using JavaScript or jQuery? 使用<a>标记和onclick</a>使用javascript更改页面元素的样式<a>?</a> - Changing a page element's style with javascript using the <a> tag and onclick? 如何在 JavaScript 中设置元素的 style.top 属性? - How do I set the style.top property of an element in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM