[英]How to select an image src with div id via javascript
For example I have this例如我有这个
<div id="p22" class="ibox-content product-box" onclick="selectPlace('22')">
<img src="https://lh3.googleusercontent.com/7LYKrgJm_cuZ5eldVYbPJBrp3ZI8I2jhtkBaLbKkLa0SYNxKqst4R22GKTQMrMVrhiNpMgQYlUcnG_7p=s0" alt="" class="image-create-site">
<div id="check-p22" class="float-right top" style="display: none">
<i class="float-right fa fa-check-circle fa-2x text-navy"></i>
</div>
<div class="product-desc">
<span class="product-name"> 美容室 amor 水天宮前店(アモール)</span>
</div>
</div>
These images are generated from an API and can change from time to time, so I need to get them based on the id of the div to send them through a form这些图像是从 API 生成的,并且会不时更改,因此我需要根据 div 的 id 获取它们以通过表单发送它们
in that example the id is "p22", can be obtained by calling to selectedplace
在该示例中,id 是“p22”,可以通过调用
selectedplace
来获得
if I use console.log(selectedPlace);
如果我使用
console.log(selectedPlace);
I get 22 on the console我在控制台上得到 22
you can also do this (simplier).你也可以这样做(更简单)。
const target = document.querySelector("#p" + selectedPlace + ">img:first-child").src;
I solved using this我用这个解决了
let imageSelected = document.getElementById('p' + selectedPlace).getElementsByTagName('img').item(0).getAttribute('src');
console.log(imageSelected);
It works with all IDs它适用于所有 ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.