Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery插件出错_Javascript_Jquery_Dom_Plugins - Fatal编程技术网

Javascript jQuery插件出错

Javascript jQuery插件出错,javascript,jquery,dom,plugins,Javascript,Jquery,Dom,Plugins,我正在使用jQuery1.9.1,并尝试开发一个插件。问题是插件不起作用。代码如下: ;(function($) { $.fn.single = function() { return this.each(function(){ // Get the instance var element = $(this); // Resize the "data-target" divs

我正在使用jQuery1.9.1,并尝试开发一个插件。问题是插件不起作用。代码如下:

    ;(function($) {

    $.fn.single = function() {

        return this.each(function(){

            // Get the instance
            var element = $(this);

            // Resize the "data-target" divs
            element.load(function(){
                changeCSS(element);
            });

            // Bind the method to the resize window event
            $(window).bind("resize", function(){  
                changeCSS(element);  
            });

        });

    };

    // function to resize all the "data-target" divs
    function changeCSS(element) {

        // Grab the screen resolution
        var windowWidth     = $(window).width();
        var windowHeight    = $(window).height();
        // Count how many targets the div has
        var targetsSize     = $("[data-target]").size();

        // Resize the parent div
        $(element).css({
            "width" : windowWidth,
            "height": windowHeight * targetsSize
        });

        // Resize all the targets div
        $(element + "> div[data-target]").each(function(){
            $(this).css({
                "width" : windowWidth,
                "height": windowHeight
            });
        });

    }

})(jQuery);
我在文件上这样称呼它:

<script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/single-0.1.0.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#single").single();
        });
    </script>

$(文档).ready(函数(){
$(“#single”).single();
});

控制台中没有问题。我做错了什么?

我假设这是因为您误用了方法.load。如果您查看jQuery文档,它将用于:

.load():从服务器加载数据并放置返回的HTML 进入匹配的元素

删除行
元素。加载(函数…
),只需调用changeCSS函数,您已经在文档上加载了此扩展。准备好了吗

    return this.each(function () {
        // ...

        changeCSS(element); // <-- just run the function right away

        // ... etc
    });
返回此.each(函数(){
// ...

更改CSS(元素);//一般来说,在函数声明之前调用函数是不好的做法。为了使插件正确,最好从一开始就正确地构造它。此外,正如@mcpDESIGNS所指出的,您错误地使用了.load方法。此外,如果您解释一下您正在尝试的是什么,这将非常有用这是我的同谋

为了让jQuery插件有一个良好的开端,请尝试查看jQuery的文档,或者您可以查看教程

这是首选结构:

(function($) {

  // Declare your methods at the beginning of your plugin
  var yourMethods = {
    'methodOne' : function() {

    },
    'methodTwo' : function() {

    }
  };

  $.fn.pluginName = function() {

    return this.each(function() {

    });

  }
}(jQuery));
而不是:

element.load(function(){
    changeCSS(element);
});
$(element + "> div[data-target]")
我做到了:

changeCSS(element);
$(element).children('div[data-target]')
和…而不是:

element.load(function(){
    changeCSS(element);
});
$(element + "> div[data-target]")
我做到了:

changeCSS(element);
$(element).children('div[data-target]')
现在插件正在执行中,没有bug或错误。
谢谢大家!

你们到底想做什么,还有什么不起作用呢?这个:
$(element+”>div[data target])
应该是
$(element)。children('div[data target'))
“问题是插件不起作用。”这并不是一个有用的描述。请努力一点。@Pointy谢谢,你的提示是问题之一。非常感谢!你和@Pointy帮了我很多忙!