简体   繁体   English

之后/之前的jQuery addClass

[英]jQuery addClass after / Before

<div id="box"></div>
<div class="text"></div>?



$(document).ready(function () { 
$('#box').click(function () {
      $('.text').slideToggle('slow');
  });


});


#box{
height:40px;
width:100px;
background:red;
}

#box:hover{background:blue;}

#box:after{
content: "";
height: 10px;
position: absolute;
width: 0;
margin-top:40px;
margin-left:40px;
border: 10px solid transparent;
border-top-color: red;
}


#box:hover:after{

border-top-color: blue;
}

.text{

display:none;
height:40px;
width:100px;
background:#fd0;
margin-top:20px;
}

http://jsfiddle.net/zfQvD/16/ http://jsfiddle.net/zfQvD/16/

Is that possible to use jQuery to add the styled after arrow? 是否可以使用jQuery添加样式after箭头? when text class is closed, remove after arrow class if text class is shown, then add the arrow? text class关闭时,如果显示text class ,则after箭头类after删除,然后添加箭头? I tried, but seems doesn't work 我试过,但似乎不起作用

As per @elclanrs comment, you can't add pseudo elements with JS. 根据@elclanrs的评论,你不能用JS添加伪元素。 What you can do, however, is declare them in your CSS to be only shown when a parent element has a specific class, and then toggle this class with JS, like this: 但是,您可以做的是在CSS中声明它们仅在父元素具有特定类时显示,然后使用JS切换此类,如下所示:

#box.expanded:after {
 content:'';
 height: 10px;
 position: absolute;
 width: 0;
 margin-top:40px;
 margin-left:40px;
 border: 10px solid transparent;
 border-top-color: red;
}

You also have to add a line to your JS for this class to be added upon clicking the box: 您还需要在JS中添加一行,以便在单击该框时添加此类:

$('#box').click(function () {
  $(this).toggleClass('expanded');
  $('.text').slideToggle('slow');
});

See the example here: http://jsfiddle.net/zfQvD/17/ 请参阅此处的示例: http//jsfiddle.net/zfQvD/17/

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

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