我有以下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>
...
我正在浏览这个for循环的链接:
var x = document.getElementsByClassName("link-d");
for (i = 0; i < x.length; i++) {
x[i].href = link;
}
现在我的问题是我如何保存链接中的图像的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;
}
我知道我可以遍历custom-below-image
,但这不是我需要的,我正在寻找一个纯JavaScript没有jQuery解决方案的任何想法? THX
答案 0 :(得分:3)
如果type cache = {
code: string;
name: string;
state: string;
(...)
hiddenDate: string;
(...)
altitude: int
} ;;
val hiddenDateSort: cache list -> cache list
标记第一个子元素是图像,那么
a
OR
var link_of_image_inside_node = x[i].firstChild.src;
其中var link_of_image_inside_node = x[i].children[0].src; //ignores all the text before img tag
是锚元素