javascript - this.call 不是函数

标签 javascript jquery css

在我的一生中,我看不到我错过了什么。我在 drawmenu 函数中硬编码了 selectedIcon 的值。在我将其更改为参数之前,它一直运行良好。我收到以下错误:

Error: this.call is not a function

Source File: http://localhost:9090/tests/jquery-1.3.2.min.js

Line: 19

例程是绘制一个菜单,同时动态调整图像之间的间距。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta name="generator" content="HTML Tidy for Windows (vers 14 February 2006), see www.w3.org" />
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>
        Test image overlay
    </title>
    <script type="text/javascript" src="jquery-1.3.2.min.js">
    </script>
    <script type="text/javascript">
        //<![CDATA[

        function drawmenu (selectedIcon) {

            var offset = 0;
            for (x in icons) {
                // Add the gap at the top of the image
                if (selectedIcon-1 == x) {
                    offset += icons[x].topBig;
                    $("#"+icons[x].divid).toggleClass("active");
                    $("#"+icons[x].imgid).toggleClass("rollit");
                } else {
                    offset += icons[x].topSmall;
                }

                // Set the location of the image
                $("#"+icons[x].divid).css("top",-offset);

                // Add the gap at the bottom of the image
                if (selectedIcon-1 == x) {
                    offset += icons[x].bottomBig;
                } else {
                    offset += icons[x].bottomSmall;
                }
                $("#"+icons[x].imgid).attr("src",icons[x].image);
            }
        }

        var icons = [
            {
                "divid"       : 'icon1', // Home
                "imgid"       : 'imgicon1',
                "image"       : 'home.png',
                "topSmall"        : 5,
                "topBig"          : 5,
                "bottomSmall" : 5,
                "bottomBig"   : 7
            },{
                "divid"       : 'icon2', // Alert
                "imgid"       : 'imgicon2',
                "image"       : 'alert.png',
                "topSmall"    : 7,
                "topBig"      : 13,
                "bottomSmall" : 0,
                "bottomBig"   : 0
            },{
                "divid"       : 'icon3', // Question
                "imgid"       : 'imgicon3',
                "image"       : 'question.png',
                "topSmall"    : 4,
                "topBig"      : 8,
                "bottomSmall" : 5,
                "bottomBig"   : 7
            },{
                "divid"       : 'icon4', // Lightbulb
                "imgid"       : 'imgicon4',
                "image"       : 'lightbulb.png',
                "topSmall"        : 3,
                "topBig"          : 7,
                "bottomSmall" : 5,
                "bottomBig"   : 7
            },{
                "divid"       : 'icon5', // Blog
                "imgid"       : 'imgicon5',
                "image"       : 'blog.png',
                "topSmall"        : 3,
                "topBig"          : 6,
                "bottomSmall" : 1,
                "bottomBig"   : 4
            },{
                "divid"       : 'icon6', // Defect
                "imgid"       : 'imgicon6',
                "image"       : 'defect.png',
                "topSmall"        : 7,
                "topBig"          : 10,
                "bottomSmall" : 0,
                "bottomBig"   : 0
            }]

        $(document).ready(drawmenu(6));
        //]]>
    </script>
    <style type="text/css">
        /*<![CDATA[*/
        #iconstack {
            position: absolute;
            top:30px;left:100px;
        }
        .icondiv { z-index:1; position: relative;}

        .active { height:80px; }

        .rollit {
            height:50px;
        }

        .rollit:hover {
            background-image:url('highlight.png');
            background-color:red;

        }

        /*]]>*/
    </style>
</head>
<body>
<div id="iconstack">
    <div id="icon1" class="icondiv">
        <img id="imgicon1" class="rollit" src="" alt="" />
    </div>
    <div id="icon2" class="icondiv">
        <img id="imgicon2" class="rollit" src="" alt="" />
    </div>
    <div id="icon3" class="icondiv">
        <img id="imgicon3" class="rollit" src="" alt="" />
    </div>
    <div id="icon4" class="icondiv">
        <img id="imgicon4" class="rollit" src="" alt="" />
    </div>
    <div id="icon5" class="icondiv">
        <img id="imgicon5" class="rollit" src="" alt="" />
    </div>
    <div id="icon6" class="icondiv">
        <img id="imgicon6" class="rollit" src="" alt="" />
    </div>
</div>
</body>
</html>

最佳答案

这实际上是一个小问题。你之前做的是这样的:

$(document).ready(drawmenu);

传递了一个函数。你现在正在做的是:

$(document).ready(drawmenu(6));

第二种方式是传递函数的结果而不是函数本身,等价于:

var item = drawmenu(6);
$(document).ready(item);

改为尝试:

$(document).ready(function(){ drawmenu(6); });

关于javascript - this.call 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1805121/

相关文章:

php - 拉拉维尔 5 : jQuery Typeahead gives error status 500 on page load (Missing argument)

javascript - 复选框不起作用

html - 使 flexbox 行占据浏览器高度的 100%

javascript - "Converting"jQuery 到纯 JavaScript

javascript - 检查表单字段中的值是否超过某个数字

javascript - Process.nextTick 和 Promise 回调

javascript - 似乎无法正确链接外部 JavaScript

javascript - HTML5 Canvas 替代 putImageData

javascript - jQuery datepicker 开始日期间隔不显示

html - 在右侧放置许多不同的 div 元素,每个元素的高度和宽度均为 100%(全屏)