Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

js 找到ul>li>div>img里的src属性

image.png

var ul = document.getElementById('goodsImg');
var lis = ul.getElementsByTagName('li');
console.log(1111111111, ul);
console.log(2222222222, lis);
console.log(2323, lis.length);
console.log(3333333333,lis[0])

希望找到11111111里,ul>li>div>img的src,2222222222里可以看到li下面是有一个结果,但是2323里却显示length=0,所以这种样式的该如何才能找到src呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. 首先,控制台打印的是快照,也就是说他是会变的。
  2. 其次,他是 HTMLCollection 对象,这个东西是个实时的。
  3. 然后就是你说你用的jquery。那么为啥要用原生去获取呢?

这种问题查到最后,肯定还是因为异步数据导致的

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...