LoginSignup
1
1

More than 5 years have passed since last update.

aPartHide jQuery Pluginを修正して使う

Posted at

ulタグで「続きを読む」を実装したかったのだが、
aPartHide jQuery Pluginでは.live()でエラーが出た。

問題の箇所

38行目と41行目。

jquery.aparthide-0.2.js(L36-L46)
if ($element.find(plugin.settings.listElement+" "+plugin.settings.listItems).length > plugin.settings.hidingTriggerLength) { // L36
  plugin.hideElements(); // L37
  $element.find('.'+plugin.settings.controlElementShowClass).live('click',function() { // L38
    plugin.showElements(); // L39
  }); // L40
  $element.find('.'+plugin.settings.controlElementHideClass).live('click',function() { // L41
    plugin.hideElements(); // L42
  }); // L43
} else { // L44
  $element.find(plugin.settings.listControlElement).hide(); // L45
} // L46

.live()関数の置き換え方法

.live()関数は、

version deprecated: 1.7, removed: 1.9

とある通り、jQuery1.9で削除されたので、他の関数を使って書き換える必要がある。
修正方法はそのサイトに載っていたので以下にそのまま引用。

Before

$(selector).live(events, data, handler);

After

$(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
$(document).on(events, selector, data, handler);        // jQuery 1.7+

これに従って、コードを修正する。

修正後

修正後はこんな風になる。

jquery.aparthide-0.2.js(L38、修正後)
  $(document).on('click', '.'+plugin.settings.controlElementShowClass, function() { 
jquery.aparthide-0.2.js(L41、修正後)
  $(document).on('click', '.'+plugin.settings.controlElementHideClass, function() { 

$element.find('.'+plugin.settings.controlElementShowClass)をそのままセレクタとして突っ込むと動かないので注意。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1