0

すべての同じクラスではなく、特定のクラスのみに結果を反映するjqueryプラグインを作成しようとしています

jQuery プラグイン

(function( $ ){
  $.fn.tooltip = function( options ) {    
    var settings = $.extend( {
      'content'         : 'top'      
    }, options);    
    return this.each(function() {       
      $(this).html(settings.content);
    });
  };
})( jQuery );

呼び出しプラグイン

<body>

<div class="toolbar"></div>

<script type="text/javascript">
$('.toolbar').tooltip({
    content:'this is first'
});
</script>

<div class="toolbar"></div>
<script type="text/javascript">
$('.toolbar').tooltip({
    content:'this is second'
});
</script>

</body>

私は結果を期待しました:

this is first   
this is second

しかし、結果表示:

this is second
this is second

では、特定のクラスのみを反映するプラグインを作成する方法。

4

1 に答える 1

2

クラスを追加して、プラグインに、このような前にこのタグを既に実行したことを伝えます

(function( $ ){
  $.fn.tooltip = function( options ) {    
    var settings = $.extend( {
      'content'         : 'top'      
    }, options);    
    return this.not('.tooltiped').each(function() {       
       $(this).html(settings.content);
    }).addClass('tooltiped');
  };
})( jQuery );

例:

http://jsfiddle.net/k7m6x/

アップデート

要素のSheikh Heeraコンテンツのみを変更する場合は、次のコードで十分です。

  return this.not('.tooltiped').html(settings.content).addClass('tooltiped');
于 2013-09-24T04:49:34.083 に答える