简体   繁体   English

如何使用javascript将类添加为元素的字符串?

[英]how can i add class to an element as string with javascript?

how can I add a class to my html element as a string 如何将一个类作为字符串添加到我的html元素中

like this: 像这样:

'.step1{transform : translate(10px,10px); transition-duration: 1s;}'

I've tried jquery addClass but it takes only the class name not a string as the whole class. 我试过jquery addClass但它只使用类名而不是整个类的字符串。

the problem is i want to generate a class dynamically then remove it using removeClass, if i add it as css it's not possible to remove it easly 问题是我想动态生成一个类,然后使用removeClass删除它,如果我将其添加为css,则无法轻松删除它

This should do the trick. 这应该可以解决问题。 If you hover the second box, the first one moves. 如果将鼠标悬停在第二个框上,则第一个框会移动。

 $('.two').mouseenter(function(){ $('.one').addClass('move'); }); $('.two').mouseleave(function(){ $('.one').removeClass('move'); }); 
 .one, .two { background-color:green; width:100px; height:100px; transition-duration: 1s; } .two { background-color:red; } .move { transform: translate(100px, 100px); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='one'> </div> <div class='two'> </div> 

This might help you, This will not add any class , it will directly add the styling to the div . 这可能会为您提供帮助,这不会添加任何class ,它将样式直接添加到div addClass() / removeClass() will only use for adding/removing the class . addClass() / removeClass()仅用于添加/删除class The parameter passing inside this will be a class name. 传递给它的参数将是一个class名。

$('.targetDiv').CSS({"transform" : "translate(10px,10px)", "transition-duration": "1s"});

If you are already using Jquery then use css() function Know more here 如果您已经在使用Jquery,请使用css() function 在此处了解更多

i hope it helps. 我希望这会有所帮助。

$(".step1").css({"transform": "translate(10px,10px)", "transition-duration": "1s"});

I've added two examples. 我添加了两个示例。
One with adding a class to the div and placing the style in it. 将一个添加到div并在其中放置样式。 or I've added a Jquery function that applies the css to the div without adding a class. 或者我添加了一个Jquery函数,该函数 CSS 应用于 div 而不添加类。

jsfiddle here jsfiddle在这里

 $('#add_class').on('click', function(){ $("#my_first_div").addClass("step1"); }); $("#add_css").on('click', function(){ $("#my_first_div").css("transform", "translate(10px,10px)"); $("#my_first_div").css("transition-duration", "1s"); }); $("#reset").on('click', function(){ $("#my_first_div").removeClass("step1"); $("#my_first_div").css("transform", "translate(0px,0px)"); $("#my_first_div").css("transition-duration", "1s"); }); 
 #my_first_div { color:red; } .step1{transform : translate(10px,10px); transition-duration: 1s;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <div id="my_first_div"> my div </div> <br /><br /><button id="add_class"> add class </button> <button id="add_css"> add css </button> <button id="reset"> reset </button> 

explanation: with the addClass event a class will be added to the div and applies the style that is in the css documentation addclass 说明:通过addClass事件,一个类将添加到div中,并应用css 文档addclass中的样式

and how to remove the class 以及如何删除课程

With the css event a css style will be applied to the div without setting it in the css you can see an example and documentation here 通过css事件, 无需在css中进行设置即可css样式应用于div,您可以在此处查看示例和文档

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

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