从具有特定src属性的数组中获取图像

时间:2015-08-21 13:00:15

标签: javascript

我是js的新手。 我试图从具有类似类名的数组中获取图像 已将特定src保存在变量bigImgPath

这是我的更新代码..

  var moniqueThumbs=document.getElementsByClassName('moniqueThumbs'); // Grab the ThumbNails
var bigImagesList=document.getElementsByClassName('monique-image');  // Grabs All Big Images

var currentBigImg='';

var currentBigImageFilePath="";  // Current Big Image path Captured by current Thumb Click //s
for(var i = 0; i <  moniqueThumbs.length; i++){
     moniqueThumbs[i].addEventListener("click", grabBigImgPath); // Added a myFunction Click event to Thumbs
    }




        // Grab Big Image Path from Clicked Thumb
        function grabBigImgPath() 
        {
    currentBigImageFilePath=this.getAttribute('data-bigImgPath'); // grabs the current bigPath from the thumb//

    var currentBigImageToDisplay;
    for (var i = 0; i < bigImagesList.length; i++)
    {
        if (bigImagesList[i].getAttribute('src') == currentBigImageFilePath)
        {
            currentBigImageToDisplay = bigImagesList[i];
            console.log(currentBigImageToDisplay);

            break;
        }
    }


}

仍然没有在console.log中显示当前图像

1 个答案:

答案 0 :(得分:1)

您需要使用library(tidyr) spread(df, year, total) agecat Drug 1999 2000 1 <1 Amikacin 12 3 2 <1 Cefepime 3 6 3 <1 Ceftazidime 13 3 4 <1 Ciprofloxacin NA 4 5 1-5 Amikacin 37 52 6 1-5 Cefepime 25 34 7 1-5 Ceftazidime 38 45 8 1-5 Ciprofloxacin 38 NA 方法来获取src:

getAttribute

然而,这仍然是正确的,因为它只会返回真或假,所以你想要这样的东西:

currentBigImageToDisplay = bigImagesList[i].getAttribute('src') == bigImgPath;

或者,您可以使用var currentBigImageToDisplay; for (var i = 0; i < bigImagesList.length; i++) { if (bigImagesList[i].getAttribute('src') == bigImgPath) { currentBigImageToDisplay = bigImagesList[i]; break; } } 语法:

filter

Working JSFIddle