1

我在页面中的 jQuery 声明显然有问题:

http://www.northernvirginiapaintingcontractor.com 点击“绘制事实”

错误:“$.fn”为空或不是对象

指这一行:

$.fn.accordion = function () {

我正在将此声明用于另一个网站并且工作正常。

但是对于这个网站 - 这不能正常工作。

在网站上的一个单独页面中:GeneralContact jQuery 声明设置如下:

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

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

看来这个脚本在几个地方引用了 $: moredetail_click() hidedetail_click() based_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);
        });
    });
4

2 回答 2

0

您可以尝试这种格式,这样$将与全局范围分开,其他库不会冲突

jQuery.noConflict();
(function( $ ){
    $.fn.accordion = function () { ... };
})( jQuery );
于 2013-02-14T00:49:21.717 回答
0

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

于 2013-02-14T01:06:59.327 回答