jquery插件问题

jquery plugin problem

本文关键字:问题 插件 jquery      更新时间:2023-09-26

我正在努力找出如何正确创建jquery插件。以下是我所做的

(function( $ ){
          $.fn.myPlugin = function() {
            console.log(this.eq(0).text());
          };
        })( jQuery ); 
$('#sector1').myPlugin();

这是html

<div id="sector1">
        <span class="timestamp">1</span>
    </div>
    <div id="sector2">
        <span class="timestamp">2</span>
    </div>

问题是我无法获取sector1 span标记的文本。如何做到这一点?

(function($) {
    $.widget("ui.myplugin", {
        _init: function() {
            var text = $("#sector1 .timestamp", this.element).html();
        }
    });
})(jQuery);