Javascript 单击下拉菜单中的p元素后获取图像id
我有以下代码:Javascript 单击下拉菜单中的p元素后获取图像id,javascript,jquery,Javascript,Jquery,我有以下代码: <table class="canToggle"> <tr> <td> <div class="messagepop pop"> <p class="selection">moss</p> <p class="selection">gray</p>
<table class="canToggle">
<tr>
<td>
<div class="messagepop pop">
<p class="selection">moss</p>
<p class="selection">gray</p>
<p class="close">Cancel</p>
</div>
<img src="images/gray.jpg" class="wide high image" id="x1y1" />
</td>
...
</tr>
<table>
我想不出怎么穿过这个。我已经看了api和这里的一些问题,但是事情取决于兄弟关系。我非常感谢您的帮助。您可以使用它遍历到td
,然后查找图像
对于集合中的每个元素,通过测试元素本身并在DOM树中遍历其祖先,获取与选择器匹配的第一个元素
你也可以使用
$(this).closest('div').next('img').attr('src', $selection);
您可以使用遍历多达td
,然后查找图像
对于集合中的每个元素,通过测试元素本身并在DOM树中遍历其祖先,获取与选择器匹配的第一个元素
你也可以使用
$(this).closest('div').next('img').attr('src', $selection);
试试这个
$(document).on("click","selection",function(){
var parent = $(this).parent(); // messagepopup div
var imgId = parent.next("img:first").attr("id");
});
您可以通过parent获取img元素。下一步(“img:first”)
试试这个
$(document).on("click","selection",function(){
var parent = $(this).parent(); // messagepopup div
var imgId = parent.next("img:first").attr("id");
});
您可以通过parent获取img元素。接下来(“img:first”)
您可以尝试以下方法:
var image = $(this).parents('td').find('img').attr('src', 'url');
您可以尝试以下方法:
var image = $(this).parents('td').find('img').attr('src', 'url');
试试这个:
您可以先搜索父级“td”,然后找到图像id
$('p').click(function(){
var imageid =$(this).parents('td').find('img').attr('id');
//in image id you will get your image's id
})
试试这个:
您可以先搜索父级“td”,然后找到图像id
$('p').click(function(){
var imageid =$(this).parents('td').find('img').attr('id');
//in image id you will get your image's id
})
试试这个
$(document).on("click","selection",function(){
var parent = $(this).parent(); // messagepopup div
var imgId = parent.next("img:first").attr("id");
});
$(“p”)。单击(函数(){
警报($(this.parent().sibbins().attr(“id”));
})
苔藓
灰色
取消
试试这个
$(document).on("click","selection",function(){
var parent = $(this).parent(); // messagepopup div
var imgId = parent.next("img:first").attr("id");
});
$(“p”)。单击(函数(){
警报($(this.parent().sibbins().attr(“id”));
})
苔藓
灰色
取消
哇,真是太棒了!非常感谢。我一直在做这件事,但从来没有做对。哇,真的!非常感谢。我一直在做这件事,但从来没有做对。