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内img的src

一个放大镜功能引发的问题,下面的图片是好的,但是上面的图片就这样裂了,然后报错本地引入的js undefined
image.png
image.png
F12发现src没有找到(不知道是不是因为这个所以报的上面的错)
image.png

代码是这些

<div class="magnifier" id="magnifier1">
  <div class="magnifier-container">
    <div class="images-cover"></div>
        <!--当前图片显示容器-->
       <div class="move-view"></div>
        <!--跟随鼠标移动的盒子-->
    </div>
   <div class="magnifier-assembly">
       <div class="magnifier-line">
          <ul class="clearfix animation03" id="goodsImg">
                           
           </ul>
       </div>
     <!--缩略图-->
    </div>
  <div class="magnifier-view"></div>
  <!--经过放大的图片显示容器-->
</div>
function list() {
        var str = '';
        for (var i = 0; i < imgList.length; i++) {
            str += '<li>' +
                '<div class="small-img">' +
                '<img src="' + imgList[i] + '" data="' + imgList[i] + '">' +
                '</div>' +
                '</li>';
            document.getElementById('goodsImg').innerHTML = str;
            element.render(); 
        }
    }

然后就去引入的JS里找,他是如下这么获取src的

thumbnail: ".magnifier-line > ul",//缩略图容器

var src = _this.thumbnail.find("img").eq(magnifier.index).attr('src');

然后我打印了

console.log("src1", _this.thumbnail)
console.log("src2", _this.thumbnail[0].innerHTML)
console.log("src3", _this.thumbnail[0].children)
console.log("src4", _this.thumbnail[0])

image.png

src1一层一层往下翻(这个目录下:_this.thumbnail[0].children[0].children[0].children[0].currentSrc)是能找到一个src,但是src3里多加几个children就报错了
我就想取到这个src为啥这么难....
拜托各位给点思路吧,感谢!!!


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

1 Answer

0 votes
by (71.8m points)

_this.thumbnail[0].children[0].children[0].children[0].currentSrc

我看你上面是jquery呀。为啥不能 find('img')


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

...