动态加载完成后,如何使用jquery添加类? [英] How can I add a class with jquery once dynamic loading is finished?

查看:65
本文介绍了动态加载完成后,如何使用jquery添加类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个类添加到以编程方式添加的< a> 中。当我调用 addClass 时它没有添加它,这是我假设因为插件还没有完成安装所有它的碎片,所以锚还没有。

I need to add a class to an <a> that is added programmatically. When I call addClass it doesn't add it, this is I assume because the plugin hasn't finished installing all it's bits and pieces so the anchor isn't there yet.

该页面为此处。这是顶部的登录栏。我想在登录和注册按钮中添加一个类,例如

The page is here. It's the login bar at the top. I want to add a class to the login and sign up buttons e.g.

jQuery('.bp-login a').addClass('simplemodal-login');
jQuery('.bp-signup a').addClass('simplemodal-register');

该部分的HTML是

<ul class="main-nav">
<li class="bp-login no-arrow">
    <a href="http://nicolaelvin.com/community/wp-login.php?redirect_to=http%3A%2F%2Fnicolaelvin.com%2Fcommunity">Log In
    </a>
</li>
<li class="bp-signup no-arrow">
     <a href="http://nicolaelvin.com/community/register/">Sign Up
     </a>
</li>
...</ul>


推荐答案

尝试:

jQuery(function() {

    var modal_check = setInterval(function(){

        if(jQuery('.simplemodal-login').length > 1){
            clearInterval(modal_check);
            jQuery('.bp-login a').addClass('simplemodal-login');
            jQuery('.bp-signup a').addClass('simplemodal-register');
        }

    },100);

});​

这等待直到dom准备好然后每隔10秒检查一次,看是否有模态插​​件装了。插件加载后,它会停止间隔并添加类。

This waits until the dom is ready then checks every 10th of a second to see if the modal plugin has loaded. Once the plugin has loaded it stops the interval and adds the class.

这篇关于动态加载完成后,如何使用jquery添加类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆