jQuery声明出错



我在页面中的jQuery声明遇到了一个问题:

http://www.northernvirginiapaintingcontractor.com点击"Paint Facts"

错误:"$.fn"为null或不是对象

指这一行:

$.fn.accordion = function () {

我正在为另一个网站使用此声明,效果良好。

但对于这个网站来说,这并不好用。

在网站上的另一个页面中:GeneralContactjQuery声明是这样设置的:

jQuery(document).ready(function ($) {

我没有足够的jQuery经验来调试这个,如果有人能看到问题,我将非常感激!

这个脚本似乎在几个地方引用了$:更多详细信息_click()hidetail_click()according_set()

我不确定是否所有的"$"引用都有变通办法,或者我是否应该以某种方式重写这篇文章?

        $.fn.accordion = function () {
        return this.each(function () {
            $container = $('#FactsLeftColumn');
            $container.find("dt").each(function () {
                var $header = $(this);
                var $selected = $header.next();
                $header.click(function () {
                    $('.active').removeClass('active');
                    $(this).addClass('active');
                    if ($selected.is(":visible")) {
                        $selected.animate({
                            height: 0
                        }, {
                            duration: 300,
                            complete: function () {
                                $(this).hide();
                                var id = $(this).attr("id");
                                var num;
                                if (id.length < 3) {
                                    num = id;
                                } else {
                                    num = id.substring(2, id.length);
                                }
                                $('#hidden_' + num).hide();
                                $('#more' + num).show();
                            }
                        });
                    } else {
                        $unselected = $container.find("dd:visible");
                        $selected.show();
                        var newHeight = heights[$selected.attr("id")];
                        var oldHeight = heights[$unselected.attr("id")];
                        $('<div>').animate({
                            height: 1
                        }, {
                            duration: 300,
                            step: function (now) {
                                var stepSelectedHeight = Math.round(newHeight * now);
                                $selected.height(stepSelectedHeight);
                                $unselected.height(oldHeight + Math.round((newHeight - oldHeight) * now) - Math.round(newHeight * now));
                                if ($unselected.attr("id") != null) {
                                    var id = $unselected.attr("id");
                                    var num;
                                    if (id.length < 3) {
                                        num = id;
                                    } else {
                                        num = id.substring(2, id.length);
                                    }
                                    $('#hidden_' + num).hide();
                                    $('#more' + num).show();
                                }
                            },
                            complete: function () {
                                $unselected.hide().css({
                                    height: 0
                                });
                            }
                        });
                    }
                    return false;
                });
            });
            var heights = new Object();
            $container.find("dd").each(function () {
                $this = $(this);
                $this.css("overflow", "hidden");
                heights[$this.attr("id")] = $this.height();
                $this.hide().css({
                    height: 0
                });
            });
        });
    };
    function moredetail_click(ddnum) {
        $('#view_' + ddnum).hide();
        $('#hidden_' + ddnum).show();
        var newHeight = $('#hidden_' + ddnum).height() + 2;
        if (ddnum < 5) {
            $('#' + ddnum).height(newHeight);
        } else {
            $('#Dd' + ddnum).height(newHeight);
        }
    }
    function hidedetail_click(ddnum) {
        $('#hidden_' + ddnum).hide();
        $('#view_' + ddnum).show();
        var newHeight = $('#view_' + ddnum).height() + 1;
        if (ddnum < 5) {
            $('#' + ddnum).height(newHeight);
        } else {
            $('#Dd' + ddnum).height(newHeight);
        }
    }
    function according_set() {
        $("#FactsLeftColumn").accordion();
    }
    $(document).ready(function () {
        $("#featured > ul").tabs({ fx: { opacity: "toggle"} }).tabs("rotate", 5000, true);
        var idnum = 1;
        var ddnum = 1;
        $.getJSON('FaqsJson.ashx?factType=1', function (datas) {
            var str_one = "";
            str_one = "<dl>"
            var array_len = datas.length;
            var each_num = 1;
            var answer = "";
            var split_str;
            var len, i, start_pos;
            $.each(datas, function () {
                if (this['Answer'].length > 200) {
                    split_str = this['Answer'].split(". ");
                    len = split_str.length;
                    answer = "<div id='view_" + ddnum + "'>";
                    if (split_str[0].length < 150) {
                        answer += split_str[0] + ". " + split_str[1] + ".   <a href='javascript:;' id='more" + ddnum + "' onclick='moredetail_click(" + ddnum + ")' class='moredetail'>Read More</a>";
                        start_pos = 2;
                    }
                    else {
                        answer += split_str[0] + ".   <a href='javascript:;' id='more" + ddnum + "' class='moredetail' onclick='moredetail_click(" + ddnum + ")'>Read More</a>";
                        start_pos = 1;
                    }
                    answer += "</div>";
                    answer += "<div id='hidden_" + ddnum + "' style='display:none;width:534px;'>";
                    answer += this['Answer'];
                    answer += "   <a href='javascript:;' id='more" + ddnum + "' class='moredetail' onclick='hidedetail_click(" + ddnum + ")'>Read Less</a>";
                    answer += "</div>";
                }
                else {
                    answer = this['Answer'];
                }
                str_one += "<dt class="glossquestion"><a href="javascript://" class="questionLink">" + this['Question'] + "</a></dt>";
                if (idnum < 5)
                    str_one += "<dd id="" + ddnum + "" class="glossanswer" style="right-margin:5px;width:544px;"><div class="answerbox" style="width:536px;">" + answer + "</div></dd>";
                else
                    str_one += "<dd id="Dd" + ddnum + "" class="glossanswer" style="right-margin:5px;width:544px;"><div class="answerbox" style="width:536px;">" + answer + "</div></dd>";
                idnum++;
                ddnum++;
                each_num++;
                str_one += "</dl>";
            });
            $("#glossary_first").html(str_one);
            window.setTimeout(according_set, 2000);
        });
    });

您可以尝试这种格式,这样$将与全局作用域分离,其他库不会与冲突

jQuery.noConflict();
(function( $ ){
    $.fn.accordion = function () { ... };
})( jQuery );

当您将jQuery与其他库一起使用时,请使用jQuery.noConflict();防止冲突的方法。

最新更新