如何将类作为字符串添加到我的 html 元素
像这样:
'.step1{transform : translate(10px,10px); transition-duration: 1s;}'
我试过 jquery addClass
但它只接受类名而不是字符串作为整个类。
问题是我想动态生成一个类,然后使用 removeClass 删除它,如果我将它添加为 css,则无法轻松删除它
最佳答案
这应该可以解决问题。如果将鼠标悬停在第二个框上,第一个框会移动。
$('.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>
关于javascript - 如何使用 javascript 将类作为字符串添加到元素中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38008669/