2011-08-20 40 views
2

我只是写一个简单的插件供我自己在网页中使用。我有HTML标记,如:jQuery插件不运行

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

和jQuery插件代码:

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

和我申请的插件是这样的:

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

出于某种原因警报( '测试')从未运行。但我可以看到jQuery对象已经在控制台中记录。我在这里错过了什么,感谢您的任何意见。

回答

3
(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

你忘了执行你的功能(jQuery)

this

+0

我说:‘但我可以看到jqury对象已被记录在控制台中。’ ...这意味着一切运行..除了... – bingjie2680

+0

@ bingjie2680:它不能没有(jQuery)之前工作; .... – genesis

+0

对不起,我有(jQuery)的地方。我忘了把它放在这里 – bingjie2680

0

定义jQuery插件作为

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

对不起我有(jQuery)到位。我忘了把它放在这里.. – bingjie2680

2

这个工程:

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

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

(去掉外“功能($){”)