私は次のJQコードを持っています:
$("[class^=post],[href='/u1']").prev("div").css("background-color","blue");
$("[class^=post],[href='/u3']").prev("div").css("background-color","red");
サンプルHTML:
<div id="container" style="width:500px">
<div id="p1" class="post row1">
<div class="postprofile" id="profile44" style="height:200px;width:100px;float:left;">
<dl>
<dt>
<a href="/u1"><img src="/logo_1.png" alt=""></a>
<strong style="font-size:1.2em">
<br />
<a href="/u1">RD</a>
</strong>
</dt>
<dd>...</dd>
<dd>...</dd>
</dl>
</div>
<div class="postbody" style="height:200px;width:400px;float:left;">
<div class="content-clearfix">
<div>This is a test</div>
</div>
</div>
</div>
</div>
<br />
<div id="container2" style="width:500px">
<div id="p2" class="post row3">
<div class="postprofile" id="profile45" style="height:200px;width:100px;float:left;">
<dl>
<dt>
<a href="/u3"><img src="/logo_2.png" alt=""></a>
<strong style="font-size:1.2em">
<br />
<a href="/u3">Evilyynn</a>
</strong>
</dt>
<dd>...</dd>
<dd>...</dd>
</dl>
</div>
<div class="postbody" style="height:200px;width:400px;float:left;">
<div class="content-clearfix">
<div>This is also a test</div>
</div>
</div>
</div>
</div>
タグ内のhrefに基づいてpostprofiledivの背景色を変更して、すべての/ u1 hrefの背景が赤になり、すべての/u3の背景が青になるようにします...
私の知る限りでは、上部のJQコードが最も簡単ですが、現在、post、hrefが/u1、またはhrefが/ u3で始まるクラスを持つすべての要素を選択するため、すべてを実行するとpostprofiledivの背景は赤です。post AND a href of / u1で始まるすべてのクラス、またはpost AND a href of/u3で始まるクラスを選択するにはどうすればよいですか。
JQ関数は、HTML本体がロードされるときに呼び出されます。