この方法を試すことができます:
$(function () {
$('.picker').css('background-color', function () { //Setup the background color for each picker square.
return $(this).data('color'); //get its color from data attribute
}).click(function () { //Chain it through for the click event
$(this).closest('.content').css({ //get the parent content element where color needs to be applied
color: $(this).data('color') //set the color as that of the clicked picker.
});
});
});
content
マークアップで、独自の親コンテンツを識別するためにsay というクラスを提供します。
<div class='col2 content' id="test1"> <!-- You don't need id if you are using this for selcting.
picker1, 2
すべてのCSS ルールを削除し、 picker
.
を使用closest
すると、ピッカーにラッパーを追加する予定がある場合でも、実際のコンテンツ div が選択されます。