简体   繁体   English

根据子项操作父项

[英]Manipulating parent div elment on basis of its child div elment

Is there a way to add any style property like 'float' to the parent div element on the basis of its child div element(preferably using javascript and without adding any extra div). 有没有办法添加任何样式属性,如“浮动”的parent div它的基础上,元素child div元素(最好是使用JavaScript和不添加任何额外的div)。

<div class="chat-message">
    <span class="chat-message-me">xxx</span>
    <span class="chat-message-content">hiyyy</span>
</div>

<div class="chat-message">
    <span class="chat-message-them">01:25 me:&nbsp;</span>
    <span class="chat-message-content">hi</span>
</div>

like in this code sample the parent divs are same but the child divs differ in class. 像此代码示例中,父div相同,但子div在类中有所不同。 So can we float the div element on its basis? 那么,我们可以在其基础上浮动div元素吗?

i suppose it would require the 'if' statement but i am unable to do it.. 我想这将需要'if'语句,但我无法做到。

please help. 请帮忙。

Thanx! 感谢名单!

Try This 尝试这个

 $('.chat-message-me').parent('div').addClass('newClass');

Have a Look 看一看

StackOverflow Answer StackOverflow答案

in jQuery: 在jQuery中:

$('chat-message-them').parent().addClass('left');

CSS CSS

.left {float:left}

If you used sass, this is easily possible. 如果您使用了无礼,这很容易实现。 For example this bit of css (in a sass sheet) will target the parent directories: 例如,这部分css(在一个sass表中)将针对父目录:

.chat-message-me { 
  .chat-message & {
    float: left;
  }
}
.chat-message-from { 
  .chat-message & {
    float: right;
  }
}

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

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