简体   繁体   English

jQuery .addclass在wordpress中不起作用

[英]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;"> 我有以下div ,它使用<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: 我正在使用以下内容,但无法正常工作,但是我已经在控制台中运行了它,它给了我我想要的答案-WP已加载JQ,因为还有其他插件等都可以正常工作:

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. $(document).ready()$(window).load()仅在DOM准备就绪时和页面首次加载时触发一次。 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 不能保证在#header-sticky-wrapper进入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. 如果通过jQuery.load()函数将新内容放入DOM ,则jQuery将执行存在该内容的任何脚本。 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)

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

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