开发者

jQuery plugin does not run

I am just writing a simple plugin for my own use in a web page. I have html markup like:

<ul id="ulview">
    <li>...</li>
    <li>...</li>
    ...
</ul>

and jQuery plugin code:

(function($) {
    $.fn.ulView = function(){
       //console.log(this);
       this.children('li').each(function(){
           alert('test');
       });
    }
})(jQuery);

and I apply the plugin like this:

jQuery(document).ready(function($) {
    $('#ulview').ulView();
}

For some reason the alert('test') never runs. but I can see the jQuery ob开发者_如何学编程ject has been logged in the console. what do I miss here, thanks for any input.


(function($) {
    $.fn.ulView = function(){
       //console.log(this);
       this.children('li').each(function(){
           alert('test');
       });
    }
})(jQuery);

$(function(){

    $('#ulview').ulView();
});

http://sandbox.phpcode.eu/g/69be3.php

you forgot to execute your function (jQuery)

Read this


this works :

  $.fn.ulView = function(){
       //console.log(this);
       this.children('li').each(function(){
           alert('test');
       });
    }

$('#ulview').ulView();

(remove the external "function($) { ")


define the jquery plugin as

(function($) {
    $.fn.ulView = function(){
       //console.log(this);
       $(this).children('li').each(function(){
           alert('test');
       });
    }
})(jQuery);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜