繁体   English   中英

jQuery TypeError:$不是函数

[英]Jquery TypeError: $ is not a function

我得到了这个脚本:

<script>
    $(document).ready(function () {
        $('a[@href^= ""] img').parent().click(function () {
            var linkz = $(this).attr("href");
            if(linkz.toLowerCase().indexOf("http: //www.website.com") >= 0) {
                window.open($(this).attr("href"));
                return false;
            } else {
                window.open("http://www.website.com/p/img.html?img=" + $(this).attr("href "));
                return false;
            }
        });
    });
</script>

要在新页面中打开所有图像,并在新链接中传递图像网址。 但我越来越

TypeError: $ is not a function.

我试图添加jQuery(document)而不是$(document),但是我得到了

$(&#39;a[@href^=&quot;&quot;] img&#39;) TypeError:$不是函数

这里。

您要么不包括jQuery,要么运行noConflict()并释放$的控制权。

http://api.jquery.com/jQuery.noConflict/

如果使用noConflict,则只需使用jQuery(),包括jQuery('a [@ href ^ =“”] img')。

您似乎未正确包含jQuery,因为浏览器不知道$是什么。 确保在<head>中包含这样的内容以包含jQuery:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

您还没有添加jQuery,请在<head></head>处添加它

  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

如果您正在本地测试,并且带宽较低,则将脚本** http://code.jquery.com/jquery-1.9.1.min.js **一次下载到您的文件夹中并使用
<script src="jquery-1.9.1.min.js"></script>

尝试这个;

<script language="JavaScript" type="text/javascript" src="jquery/jquery.js"></script>
<script>
    jQuery.noConflict();
    (function ($) {
        function readyFn() {
            // Set your code here!!
        }

        $(document).ready(readyFn); 
    })(jQuery);

</script>

或您的情况:

<script language="JavaScript" type="text/javascript" src="jquery/jquery.js"></script>
<script>
    jQuery.noConflict();
    (function ($) {
        function readyFn() {
             $('a[@href^= ""] img').parent().click(function () {
             var linkz = $(this).attr("href");
             if(linkz.toLowerCase().indexOf("http: //www.website.com") >= 0) {
                 window.open($(this).attr("href"));
                 return false;
             } else {
                 window.open("http://www.website.com/p/img.html?img=" + $(this).attr("href"));
                 return false;
             }
        });
        }

        $(document).ready(readyFn); 
    })(jQuery);

</script>

暂无
暂无

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

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