Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么要使用这个简单的jQuery';addClass';是否不在此站点中工作?_Javascript_Jquery_Wordpress_Lazy Loading - Fatal编程技术网

Javascript 为什么要使用这个简单的jQuery';addClass';是否不在此站点中工作?

Javascript 为什么要使用这个简单的jQuery';addClass';是否不在此站点中工作?,javascript,jquery,wordpress,lazy-loading,Javascript,Jquery,Wordpress,Lazy Loading,我已经测试了jQuery addClass: 那很好 jQuery将名为lazy的类添加到图像中 现在我想在wordpress网站上做同样的事情。 因此,我在functions.php function smart_magazine_scripts_styles() { wp_enqueue_script('lazyloadscriot', get_template_directory_uri() . '/js/jquery.lazyload-any.min.js', arra

我已经测试了jQuery addClass: 那很好

jQuery将名为
lazy
的类添加到图像中

现在我想在wordpress网站上做同样的事情。 因此,我在
functions.php

function smart_magazine_scripts_styles() { 
        wp_enqueue_script('lazyloadscriot', get_template_directory_uri() . '/js/jquery.lazyload-any.min.js', array(), '1.0', true);
        wp_enqueue_script('custom-by', get_template_directory_uri() . '/js/custom-by.js', array(), '1.0', true);
    }
当我看到站点源代码时,我看到
lazyload any.min.js
custom by.js
都加载得很好

custom-by.js仅包含以下行:

jQuery('img').addClass('lazy');
但是当我检查图像时,没有添加名为
lazy
的类

为什么呢?
这是答案,你试过这个了吗

$(function()
{
    $('img').addClass('lazy');
});
如果您试图以类img为目标元素,则需要使用.img;如果您试图以id img为目标元素,则需要使用#img

要从Google加载jQuery,只需将这一行放在HTML文件中

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

您确定您的文档准备好了吗?请尝试以下操作:

jQuery(document).ready(function() {
     jQuery('img').addClass('lazy');
});

执行函数时,jQuery可能还没有加载。 首先,尝试将JavaScript放入$(function()中,如下所示:

jQuery(function(){
  jQuery('img').addClass('lazy');
});

当然,如果您等待DOM准备就绪,然后将类lazy添加到您的图像中,则没有任何意义。最好是在静态模板中或使用PHP添加该类。

是否加载了jQuery本身?是的。网站已经为其滑块添加了jQuery。因此,我认为无需再次添加jQuery。.Firebug说..“jQuery未定义”尝试代替jQuery->$打开浏览器控制台并检查…jQuery根本不工作。这就是为什么。但是正如你看到的jQuery在这里:
js/jQuery/jQuery.js?ver=1.11.1
yup..我也尝试过:
jQuery(document).ready(function(){jQuery('img').addClass('lazy');});
但是你仍然可以试试这个:jQuery吗(document).ready(函数(){console.log('jquery已准备好使用'));你在控制台里看到什么了吗?我知道了。那些没有添加类的图像都来自外部站点。你能解释一下吗?即使图像是外部图像,它也应该接收正确的类。也许lazyload不能与外部资源一起工作?噢。但这不是关于
lazyload
现在的问题。我认为它不起作用是因为来自外部站点的se图像和整个HTML块仍然是一样的。但是人们开始投反对票,即使他们不说解决方案或原因。。