简体   繁体   English

addClass 发生后如何添加内容

[英]How to add something after addClass happen

I have div and button.我有 div 和按钮。 when i click button this div adds class and its width become 150px;当我单击按钮时,此 div 添加 class 并且其宽度变为 150px; I want that when this div become 150px show something in console.log.我希望当这个 div 变成 150px 时在 console.log 中显示一些东西。 but this console dont be shown until div becomes 150px;但是在 div 变为 150px 之前不会显示此控制台; can someone help me please?有人能帮助我吗? can u show me code example?你可以给我看代码示例吗? here is code what i have now:这是我现在拥有的代码:

html: html:

<div class="test"></div>
<button class="button">BUTTON</button>

Js: JS:

$('.button').click(function(){
$( ".test" ).addClass( "addTest" );
})

CSS: CSS:

.test{
width:0px;
}
.addTest{
   width:150px;
}

There are two possibilities:有两种可能:

Exactly what you asked for, can be achieved by just adding the console.log() command after you call the addClass:正是您所要求的,只需在调用 addClass 后添加 console.log() 命令即可实现:

$(".button").click(function(){
    $( ".test" ).addClass( "addTest" );
    console.log("Test!");
})

But I assume you want to react on the size of the element, without regarding what changed the size?但我假设您想对元素的大小做出反应,而不考虑是什么改变了大小? In this case, you need to introduce some additional framework.在这种情况下,您需要引入一些额外的框架。 Have a look here: How to detect DIV's dimension changed?看看这里: 如何检测 DIV 的尺寸变化?

According to your update in the comments.根据您在评论中的更新。 I'd go like this:我会像这样 go :

$(".button").click(function(){
    $( ".test" ).addClass( "addTest" );
    if($( ".test" ).css("margin-left")=="180px"){
        console.log("Test!");
    }
})

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

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