简体   繁体   中英

jQuery .addclass not working in wordpress

I have the following div and it shows a sticky header using <div id="header-sticky-wrapper" class="sticky-wrapper is-sticky" style="height: 205px;">

How could I append another class to this line?

I am using the following but its not working but I have run it in the console and it gives me my answer I am wanting - WP has JQ loaded as there are other plugins etc that are working fine:

jQuery(document).ready(function($) {
    $('#header-sticky-wrapper').addClass( 'nonHomeLogo' );
});

$(document).ready() and $(window).load() fire only once when the DOM is ready and when the page first loads respectively. Using it in a script that will be loaded dynamically has no effect.

You can set a timeout to execute your script

setTimeout(function(){
  $('#header-sticky-wrapper').addClass( 'nonHomeLogo' );
}, 1000);

Which is not guaranteed to be executed before your #header-sticky-wrapper enters the DOM

If the new content is put into the DOM via the jQuery.load() function, jQuery will execute any script that exists it that content. In that case, you could add your script somewhere after the element in question.

<div id="header-sticky-wrapper">...</div>
<script> $('#header-sticky-wrapper').addClass( 'nonHomeLogo' );</script>

try this code:

 (function($) {
    $('#header-sticky-wrapper').addClass( 'nonHomeLogo' );
    })(jQuery)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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