これが可能かどうかはわかりません。複数の要素を選択するのは簡単ですが、今日、これが可能かどうかは疑問です:
$(this).closest("li, + $(this)").css('text-decoration','line-through');
上記のコードは確かに機能しません。アイデアのデモ用です。li
全体(テキストとチェックボックス)を打ちたいので、li
と一緒に選択しましたcheckbox
。
$(this).closest("li").andSelf().css('text-decoration','line-through');
また
$(this).closest("li").addBack().css('text-decoration','line-through');
jQuery 1.8 以降の場合
$(this).closest("li").add(this).css('text-decoration','line-through');
最もクリーンな解決策は、この使用法専用のaddBackを使用することです。
$(this).closest("li").addBack().css('text-decoration','line-through');
関数を使用.add()
してセレクターを追加できます。
$(this).closest("li").add(this).css('text-decoration','line-through');
あなたの $(this) はここでチェックボックスを表していると思います。それがliの中にある場合は、使用できます
$(this).parent('li').css('text-decoration','line-through');
これにより、チェックボックスとともに完全にカバーされます。
$(this).closest("li").andSelf().css('text-decoration','line-through');
を使用しandSelf
ます。
説明: スタック上の要素の以前のセットを現在のセットに追加します。
コード:
$(this).closest("li").andSelf().css('text-decoration','line-through');
ドキュメントで次のことを読みました。
注: この関数は廃止され、.addBack() のエイリアスになりました。これは、jQuery 1.8 以降で使用する必要があります。
したがってaddBack
、jQuery 1.8 以降を使用している場合に使用してください。
コード:
$(this).closest("li").addBack().css('text-decoration','line-through');