次の HTML があるとします。
<ul>
<li>
<a>Item one</a>
<small>#000000</small>
<span class="corner"></span>
</li>
<li>
<a>Item two</a>
<small>#ffffff</small>
<span class="corner"></span>
</li>
</ul>
span タグの間のテキストを取得し、同じ「li」内の「span」に背景色として適用したいと考えています。
だから私が試したのは:
jQuery(document).ready(function ($) {
$("span.corner").addClass("custom-color"); //apply class custom-color to span
if ($("span.corner").hasClass("custom-color")) { //if the span.corner has custom-color class do the next
var cornerColor = $('.corner').prev('small').text(); //this should get the text between the 'small' tags
if (cornerColor !== '') { //if there is some text between 'small' tags, apply it as a css rule to the 'corner.span'
$('.corner').css('background-color', cornerColor);
}
}
});
残念ながら機能しません。'custom-color' クラスを適用しますが、css ルールを適用しません。変数 cornerColor を実際の色に置き換えると、そうです。したがって、スパン内のテキストを取得する変数にエラーがあると思います。どんな助けでも大歓迎です。
ありがとう