简体   繁体   English

为什么我需要jquery.min.js而不是jquery.js才能使脚本起作用?

[英]Why do I need jquery.min.js vs just jquery.js for script to work?

I'm new to Wordpress and this has been bugging me for a while. 我是Wordpress的新手,这困扰了我一段时间。 I'm wondering why the code I'm trying to include only works when I include jquery.min.js in 'head' despite the child theme already including jquery.js by default. 我想知道为什么我试图包括的代码仅在我将'jhead'中包含jquery.min.js时有效,尽管默认情况下子主题已经包含了jquery.js。 Let me explain below. 让我在下面解释。

I have a script near the end of my 'body' to specify images in galleries to not have a 'pin it' button show up when hovering over them: 我在“主体”末尾附近有一个脚本,用于指定画廊中的图像,将鼠标悬停在它们上时不会显示“固定”按钮:

<script type="text/javascript">
$(document).ready(function() {
$(".tiled-gallery-item a img").attr("nopin","nopin");
});
</script>

This code only works (ie., the nopin attribute is added to the relevant img tags) when I include this in the 'head': 当我将其包含在“ head”中时,此代码仅适用(即,将nopin属性添加到相关的img标签):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript" /></script>

However, it appears from inspecting source that the child theme already includes jquery.js: 但是,从检查源看,子主题已经包含jquery.js了:

<script type="text/javascript" src="http://blog.froy.com/wp-includes/js/jquery/jquery.js?ver=1.10.2"></script>
<script type="text/javascript" src="http://blog.froy.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1"></script>

Why the heck is the jquery.min.js necessary? 为什么需要jquery.min.js? I'm new to Wordpress and it's so confusing. 我是Wordpress的新手,它是如此令人困惑。 I thought the jquery.min.js and jquery.js has no functional difference. 我认为jquery.min.js和jquery.js没有功能上的区别。 What is the jquery-migrate.min.js? 什么是jquery-migrate.min.js?

WordPress comes with built-in jQuery but doesn't load it by default. WordPress内置了jQuery,但默认情况下不会加载它。 it's probably because your theme has been applied wp_enqueue_script('jquery'); 这可能是因为您的主题已应用wp_enqueue_script('jquery'); inside functions.php 内部functions.php

One solution for you to replace this default behavior is to add this snippet to your functions.php : 替换此默认行为的一种解决方案是将该代码段添加到您的functions.php

function modify_jquery() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js', false, '1.8.1');
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'modify_jquery');

Also, because you're using use jQuery version 1.10.2 , jQuery migrate used here to detect and restore APIs or features that have been deprecated and removed as of version 1.9. 此外,因为您使用使用jQuery版本1.10.2jQuery的情况下迁移这里用来检测和恢复已被弃用,作为1.9版本中删除API或功能。

Felix provided exactly what was needed. Felix完全提供了所需的东西。 To fully answer my question for future visitors, let me explain in specifics. 为了向未来的访问者全面回答我的问题,让我详细说明。

Basically, Wordpress includes the jQuery library by default regardless of your theme (I personally use the Genesis framework with a child theme). 基本上,无论您使用哪种主题,Wordpress都默认包含jQuery库(我个人将Genesis框架与子主题一起使用)。 In addition, Wordpress, by default, sets the library to noConflict() mode. 另外,默认情况下,Wordpress noConflict()库设置为noConflict()模式。 This is to prevent compatibility problems with other JavaScript libraries that WordPress can link. 这是为了防止与WordPress可以链接的其他JavaScript库发生兼容性问题。

If you want to include any scripts that require jquery, you need to use the wp_enqueue_script() . 如果要包括任何需要jquery的脚本,则需要使用wp_enqueue_script()

Now this was the important part. 现在,这是重要的部分。 If you don't want to go through the troubles and want to include a simple, quick, script at the end of your <body> or elsewhere without having to go into functions.php and using enqueue, replace the $() shortcut with jQuery() . 如果您不想麻烦,并且想在<body>末尾或其他地方包含一个简单,快速的脚本,而不必进入functions.php并使用enqueue, 则将$()快捷方式替换为jQuery()

In my case, 就我而言

<script type="text/javascript">
$(document).ready(function() {
$(".tiled-gallery-item a img").attr("nopin","nopin");
});
</script>

needed to be... 需要...

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(".tiled-gallery-item a img").attr("nopin","nopin");
});
</script>

That's it! 而已! I was able to remove the redundant jquery.min.js and simply rely on Wordpress' default jQuery library. 我能够删除多余的jquery.min.js并仅依靠Wordpress的默认jQuery库。

I figured this out by reading this and this . 我通过阅读本本本来弄清了 一点 You can find more details in those links. 您可以在这些链接中找到更多详细信息。

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

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