简体   繁体   English

JQuery:当文档准备好时,“addClass不能通过id工作”

[英]JQuery: “addClass doesn't work by id”, when document ready

I have a problem, and try it to solve two more days. 我有一个问题,并尝试再解决两天。 Try do it like this: 尝试这样做:

$.when( $(document).ready ).then(function func_fold () {
  $( "#collapsible_sidebar" ).addClass('folded');    
})

And like this: 像这样:

$(document).ready(function func_fold () {
  $( "#collapsible_sidebar" ).addClass('folded');    
})

And like this too: 也是这样:

$(document).ready(function() {
  $( "#collapsible_sidebar" ).addClass('folded');    
})

Try .hide() to see work selector at all, and it doesn't work too. 尝试使用.hide()来查看工作选择器,它也不起作用。 But when I open DOM, I can see this element and this ID. 但是当我打开DOM时,我可以看到这个元素和这个ID。

Warning! 警告! I think it important, this div is added to DOM by AJAX. 我认为重要的是,这个div被AJAX添加到DOM中。

HTML element: HTML元素:

<div id="collapsible_sidebar"><?php dynamic_sidebar( 'sidebar-1' ); ?></div>

If it's being added to the DOM by AJAX, that means it's being added sometime after $(document).ready() . 如果它被AJAX添加到DOM中,这意味着它会在$(document).ready()之后的某个时候添加。 AJAX stands for "Asynchronous Javascript And XML", asynchronous meaning that it will execute on its own time when the server can get to it. AJAX代表“异步Javascript和XML”,异步意味着它将在服务器可以到达时自己执行。 If you want to execute that class being added, you need to put $("#collapsible_sidebar").addClass('folded'); 如果要执行要添加的类,则需要输入$("#collapsible_sidebar").addClass('folded'); into the success function in your AJAX call. 进入AJAX调用中的success函数。

$.ajax({
    //other parameters
    success: function () {
        $("#collapsible_sidebar").addClass('folded');
    }
});

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

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