在下拉菜单中单击p元素后获取图像的id
Get id of image after click on p element in drop down menu
我有这样的代码:
<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>
当我点击其中一个p
时,我需要获得img
的id,以便我可以更改img src
。我有这个JQuery:
var $selection = $(this).text();
$selection = "images/" + $selection + ".jpg";
// I need to populate the value of nextImgID for the next line
$(nextImgID).attr('src', $selection);
我不知道如何遍历这个。我看了api和一些问题,但这取决于兄弟姐妹的关系。
您可以使用.closest()
遍历到td
,然后找到图像
对于集合中的每个元素,通过测试元素本身并遍历其在DOM树中的祖先,获得与选择器匹配的第一个元素。
$(this).closest('td').find('img').attr('src', $selection);
也可以使用
$(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.next("img:first")
你可以试试:
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
})
试试这个
$("p").click(function() {
alert($(this).parent().siblings().attr("id"));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
相关文章:
- 点击(右键点击)使用传单地图库获取图像覆盖的像素坐标
- 如何使用javascript和xpath获取图像src
- 如何使用javascript从文件夹中获取图像并发布直接链接
- 如何从 javascript 中可能具有一个或多个图像的字符串中仅获取图像名称
- 获取图像节点js的主色
- 获取图像的平均外部像素颜色
- 使用asp.net FileUpload时从javascript获取图像宽度和高度
- jQuery:获取图像加载错误的详细信息
- 使用回调获取图像像素数据
- Javascript 正则表达式.获取图像源
- 获取图像宽度的最早事件
- 使用 JsonWebToken 授权标头获取图像
- 是从画布获取图像数据的任何方法,从远程视频中提取
- 铯:使用自己的OpenStreetMap服务器.:“无法获取图像磁贴”错误
- 从JSON数据中获取图像源
- 获取图像's磁盘中文件的实际高度和宽度
- 使用Jquery获取图像中心的文本
- 如何获取图像的宽度并将该值用作高度以使图像相当方正
- 在angular js和ionic中无法在服务器上捕获和上传图像以及从服务器上获取图像
- PhoneGap+JQuery_Mobile:如何通过单击按钮从照片/图像库中获取图像