<div class="gridmasonry grid_accomodation select_room">
<div class="item_grid item2">
<div class="panel">
<div class="img_preview">
<h4 class="price"><span>€20 </span>Night</h4>
<img src="~/Images/rooms/standard.jpg" alt="img_preview" />
</div>
<div class="clsRoomType"><h4>Standart Room</h4></div>
</div>
</div>
<div class="item_grid item2">
<div class="panel">
<div class="img_preview">
<h4 class="price"><span>€20 </span>Night</h4>
<img src="~/Images/rooms/luxury.jpg" alt="img_preview" />
</div>
<div class="clsRoomType"><h4>Luxury Room</h4></div>
</div>
</div>
<div class="item_grid item2">
<div class="panel">
<div class="img_preview">
<h4 class="price"><span>€20 </span>Night</h4>
<img src="~/Images/rooms/family.jpg" alt="img_preview"/>
</div>
<div class="clsRoomType"><h4>Family Room</h4></div>
</div>
</div>
<div class="item_grid item2">
<div class="panel">
<div class="img_preview">
<h4 class="price"><span>€20 </span>Night</h4>
<img src="~/Images/rooms/vip.jpg" alt="img_preview"/>
</div>
<div class="clsRoomType"><h4>VIP Room</h4></div>
</div>
</div>
<div class="item_grid item2">
<div class="panel">
<div class="img_preview">
<h4 class="price"><span>€20 </span>Night</h4>
<img src="~/Images/rooms/deluxe.jpg" alt="img_preview"/>
</div>
<div class="clsRoomType"><h4>Deluxe Room</h4></div>
</div>
</div>
<div class="item_grid item2">
<div class="panel">
<div class="img_preview">
<h4 class="price"><span>€20 </span>Night</h4>
<img src="~/Images/rooms/romantic.jpg" alt="img_preview" />
</div>
<div class="clsRoomType"><h4>Romantic Room</h4></div>
</div>
</div>
</div>
我有如上所述的DIV结构。每当我点击带有“item_grid item2”类的DIV项目时,我需要使用类“clsRoomType”获取DIV的文本
我尝试了以下代码,但它显示了所有项目的文本
$(document).ready(function () {
$(".item2").click(function () {
alert($(".item2").find('div.clsRoomType').text());
});
});
目前的结果:标准间豪华间家庭间贵宾室......
我想要每次点击每个项目。
答案 0 :(得分:3)
试试这个:
$(document).ready(function () {
$(".item2").click(function () {
alert($(this).find('div.clsRoomType').text());
});
});
答案 1 :(得分:3)
替换
alert($(".item2").find('div.clsRoomType').text());
与
alert($(this).find('div.clsRoomType').text());
这将给出单独的项目。
希望这有帮助。
答案 2 :(得分:1)
你必须在给定元素中寻找一个类
$(document).ready(function () {
$(".item2").click(function () {
alert($(this).find('div.clsRoomType').text());
});
});
答案 3 :(得分:1)
试试这个
$(document).ready(function () {
$(".item2").click(function () {
alert($(this).find('div.clsRoomType').text());
});
});