如何使用jQuery更改Wordpress中的类 [英] How to use jquery to change class in wordpress

查看:100
本文介绍了如何使用jQuery更改Wordpress中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将手风琴滑块(折叠"主题)打开到特定的幻灯片(不幸的是,折叠"没有为此提供).我已经尝试过从此代码开始可以想到的所有内容:

I'm trying to have an accordion slider (Enfold theme) open to a specific slide (unfortunately Enfold does not provide for that). I've tried everything I can think of starting with this code:

<script>
$( window ).load(function(){
$('.aviaccordion-inner').find('.aviaccordian-slide-     3').addClass('aviaccordian-active-slide');
 });
</script>

然后我也尝试了以下代码:

Then I also tried this code:

<script>
jQuery(document).ready(function( $ ) {

$( window ).load(function(){
$('.aviaccordion-inner').find('.aviaccordian-slide-    3').addClass('aviaccordian-active-slide');
});

});
</script>

我使用了(window).load"等,因为我只用简单的(document).ready"就没有运气.我确保它是在滑块和jquery加载之后加载的.我还尝试在滑块加载之前将其放在主体中.有人对我如何获取它以将特定幻灯片加载到加载"上有任何想法吗?

I used "(window).load" etc. because I had no luck either with just simple "(document).ready". I made sure it was loading after the slider and jquery load. I also tried putting it in the body just before the slider loads. Does anyone have any ideas on how I can get this to load a particular slide on 'load'?

该网站的网址为: http://www.twinsupplies.net/2017/

推荐答案

这是正确的语法,可以按类定位元素并在jQuery中向其添加类.请尝试一下,让我知道会发生什么,但是将".classYouWantToTarget"更改为您要定位的类,将"classYouWantToAdd"更改为您要添加的类名.

This is the correct syntax to target an element by class and add a class to it in jQuery. Please give this a try and let me know what happens, but change '.classYouWantToTarget' to the class you're trying to target and 'classYouWantToAdd' to the class name you're trying to add.

<script>
    $(function(){
        $('.classYouWantToTarget').addClass('classYouWantToAdd');
    });
</script>

我对动态显示元素使用的另一种策略是这样的...

Another tactic I've used on dynamically appearing elements is something like this...

<script>
$(function() {
    //Name my interval to target later
    var myInterval = setInterval(function() {
        //code from the first example
        $('.classYouWantToTarget').addClass('classYouWantToAdd');
        //See if your class has the class you're trying to add
        if($('.classYouWantToTarget').hasClass('classYouWantToAdd')) {
            //If so, clear the interval
            clearInterval(myInterval);
        }
   //Minimum acceptable interval time as of today
   }, 4);
});
</script>

您必须将jQuery添加到您的站点,否则您将在控制台中收到不是函数"错误.在您的body标签末尾之前添加此脚本.

You must have jQuery added to your site or you will get a "not a function" error in console. Add this script right before the end of your body tag.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

这篇关于如何使用jQuery更改Wordpress中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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