简体   繁体   English

使用javascript获取内部HTML元素

[英]get inner HTML element using javascript

I have the following html hierarchy: 我有以下html层次结构:

...
<div class="custom-img-wrapper">
<a target="_blank" class="link-d" href="#"><img class="custom-below-img" src="some source"></a>
<h3><a target="_blank" class="link-d" href="#">some text</a></h3>
</div>

<div class="custom-img-wrapper">
<a target="_blank" class="link-d" href="#"><img class="custom-below-img" src="some source"></a>
<h3><a target="_blank" class="link-d" href="#">some text</a></h3>
</div>
...

and i am going over the links with this for loop : 我正在检查与此for链接的链接:

var x = document.getElementsByClassName("link-d");
for (i = 0; i < x.length; i++) {
    x[i].href = link;
}

now my question is how can i save the src of the image which is inside the link, so i'll get somwthing like this: 现在我的问题是如何保存链接内图像的src ,这样我将得到如下内容:

var x = document.getElementsByClassName("link-d");
for (i = 0; i < x.length; i++) {
    var link_of_image_inside_node = x[i].????
    x[i].href = link;
}

i know i can loop over the custom-below-image but that's not what i need, and im looking for a pure JavaScript no jQuery solution any idea? 我知道我可以遍历custom-below-image但这不是我所需要的,而我正在寻找纯JavaScript没有jQuery解决方案的任何主意? thx 谢谢

If a tags first child element is image then 如果a标签的第一个子元素是图像,然后

var link_of_image_inside_node = x[i].firstChild.src;

OR 要么

var link_of_image_inside_node = x[i].children[0].src; //ignores all the text before img tag

where x[i] is the anchor element 其中x[i]是锚元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用Angular获取元素的内部HTML? - Is it possible to get the inner HTML of element using Angular? 如何使用Javascript将元素的内部HTML文本替换为数组的一部分 - How to replace inner HTML text of an element with part of an array using Javascript 使用 Javascript 更改 HTML 的内部元素以呈现 Django For 循环 - Changing the inner Element of HTML using Javascript to present Django For Loop 将html元素转换为字符串Javascript(使用搜索而非内部/外部HTML获得) - Convert html element to string Javascript (obtained using search, not inner/outerHTML) 使用属性的角度指令无法获取元素内部html - angular directive using attribute cant get element inner html JavaScript只选择不包括内部元素的外部元素的内部html - JavaScript select only inner html of outer element excluding inner element 用于分配 INNER HTML 并解析为 SVG 元素的 Javascript - Javascript to assign INNER HTML and parse to SVG element 获取内容 <title>使用JavaScript的html中的元素 - Get content of a <title> element in html using JavaScript 无法使用javascript获取html元素的颜色 - Cannot get color of html element using javascript 使用javascript获取html元素的所有属性 - get all attributes of an html element using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM