根据点击事件动态更改走马灯图片

3

我有三个元素排成一行。我想在单击行中的列时显示一个轮播弹出窗口。问题是,我无法根据所选列元素更改轮播图像。

以下是我的完整代码:

 <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      
      <style>
      .carousel-inner > .item > img,
      .carousel-inner > .item > a > img {
        width: 70%;
        margin: auto;
      }
      </style>
    </head>
    <script>
    function process() {
     var shops = JSON.parse('{ "id": "shopping", "categories": [ { "id": "Amazon", "name": "Amazon", "link": "https://images-na.ssl-images-amazon.com/images/G/01/SellerCentral/legal/amazon-logo_transparent._CB303899249_.png", "images": [ { "href": "https://images-na.ssl-images-amazon.com/images/G/01/credit/img16/CBCC/marketing/marketingpage/products._V524365396_.png" }, { "href": "http://static4.uk.businessinsider.com/image/575adbe2dd0895c4098b46ba/the-50-most-popular-products-on-amazon.jpg" } ] }, { "id": "Google", "name": "Google", "link": "http://pngimg.com/uploads/google/google_PNG19644.png", "images": [ { "href": "https://www.clipartmax.com/png/middle/147-1476512_google-google-products-logos-png.png" }, { "href": "https://xvp.akamaized.net/assets/illustrations/unblock-google/unblock-google-with-a-vpn-fc1e32f59d9c50bae315c2c8506a91e2.png" } ] }, { "id": "Apple", "name": "Apple", "link": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Apple_Logo.svg/2000px-Apple_Logo.svg.png", "images": [ { "href": "https://c.slashgear.com/wp-content/uploads/2018/03/apple-mfi-logos-update-2018-980x620.jpg" }, { "href": "https://support.apple.com/library/content/dam/edam/applecare/images/en_US/applemusic/itunes-apple-logo-apple-music-giftcard.jpg" } ] } ] }');
     var row = 1;
     var content = "";
     shops = shops.categories;
     for(var i=0; i< shops.length; i++) {
      if(row == 1) {
       content += '<div class="row">'
      }
      content += '<div class="col-md-4 col-sm-12" data-toggle="modal" onclick="processCarousel(shops[i])" data-target="#myModal">';
      content += '<img style="border: 1px solid red" src="'+shops[i].link+'" width="100%" height="100%"/></div>';
      if(row == 3) {
       row = 0;
       content += '</div>';
      } 
      row++;
     }
     document.getElementById("placeholder").innerHTML = content;
     processCarousel(); 
    }
    
    function processCarousel(input) {
     alert(input);
     var m = ['img_chania.jpg','img_chania2.jpg', 'img_flower.jpg','img_flower2.jpg'];
     var carouselInner = document.getElementById("carousel-inner");
     var carouselIndicators = document.getElementById("carousel-indicators");
     var innerContent = "";
     var indicatorsContent = "";
     for(var i=0 ; i< m.length ; i++) {
      var c = "";
      if(i == 0) {
       c = " active";
      }
      innerContent += '<div class="item'+c+'"><img src="'+m[i]+'"><div class="carousel-caption"></div>   </div>';
      indicatorsContent += '<li class='+c+'data-target="#carousel-example-generic" data-slide-to="'+i+'"></li>';
     }
     carouselInner.innerHTML = innerContent;
     carouselIndicators.innerHTML = indicatorsContent;
     var carouselExampleGeneric = document.getElementById("carousel-example-generic");
     carouselExampleGeneric.carousel();
    }
    </script>
    </html>

以上代码生成以下输出:

输入图像描述

单击任何图像会加载幻灯片,但是幻灯片中的图像是固定的,与我上面代码中提到的数组元素var m = ['img_chania.jpg','img_chania2.jpg', 'img_flower.jpg','img_flower2.jpg'];相同。

但是我想只显示存在于我的输入json shops.categories[selectedItem].images中的选定图像。

我尝试在列元素上使用onclick javascript事件,但该代码无法识别它。如何正确实现此功能?

我想使用纯javascript实现此操作。

1个回答

1

首先,您需要在第39行中取消对processCarousel();的调用。

您的主要问题是,在内容变量中传递了参数变量的字符串,而不是参数本身。请尝试使用以下代码:

content += '<div class="col-md-4 col-sm-12" data-toggle="modal" onclick="processCarousel(' + i + ')" data-target="#myModal">';

这样,您只需传递需要呈现的类别的索引。 然后,您还需要在processCarousel函数内部提供shops对象,因此我将其移到了函数范围之外。
这将导致您在processCarousel函数内部遇到进一步的问题。您必须像这样设置图像:var m = shops[i].images;而不是var m = ['img_chania.jpg','img_chania2.jpg','img_flower.jpg','img_flower2.jpg'];
这将在更深层次上再次引发错误。 'innerContent += '
';'将无法工作。相反,您必须在图像标记中使用m[i].href作为源。
现在,这将向Carousel传递配置,然后正常呈现。
您可能想考虑给变量命名,避免使用'm'这样的变量名。
var shops = JSON.parse('{ "id": "shopping", "categories": [ { "id": "Amazon", "name": "Amazon", "link": "https://images-na.ssl-images-amazon.com/images/G/01/SellerCentral/legal/amazon-logo_transparent._CB303899249_.png", "images": [ { "href": "https://images-na.ssl-images-amazon.com/images/G/01/credit/img16/CBCC/marketing/marketingpage/products._V524365396_.png" }, { "href": "http://static4.uk.businessinsider.com/image/575adbe2dd0895c4098b46ba/the-50-most-popular-products-on-amazon.jpg" } ] }, { "id": "Google", "name": "Google", "link": "http://pngimg.com/uploads/google/google_PNG19644.png", "images": [ { "href": "https://www.clipartmax.com/png/middle/147-1476512_google-google-products-logos-png.png" }, { "href": "https://xvp.akamaized.net/assets/illustrations/unblock-google/unblock-google-with-a-vpn-fc1e32f59d9c50bae315c2c8506a91e2.png" } ] }, { "id": "Apple", "name": "Apple", "link": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Apple_Logo.svg/2000px-Apple_Logo.svg.png", "images": [ { "href": "https://c.slashgear.com/wp-content/uploads/2018/03/apple-mfi-logos-update-2018-980x620.jpg" }, { "href": "https://support.apple.com/library/content/dam/edam/applecare/images/en_US/applemusic/itunes-apple-logo-apple-music-giftcard.jpg" } ] } ] }');
var row = 1;
var content = "";
shops = shops.categories;

function process() {
  for (var i = 0; i < shops.length; i++) {
    if (row == 1) {
      content += '<div class="row">'
    }
    content += '<div class="col-md-4 col-sm-12" data-toggle="modal" onclick="processCarousel(' + i + ')" data-target="#myModal">';
    content += '<img style="border: 1px solid red" src="' + shops[i].link + '" width="100%" height="100%"/></div>';
    if (row == 3) {
      row = 0;
      content += '</div>';
    }
    row++;
  }
  document.getElementById("placeholder").innerHTML = content;
}

function processCarousel(i) {
  //var m = ['img_chania.jpg', 'img_chania2.jpg', 'img_flower.jpg', 'img_flower2.jpg'];
  var m = shops[i].images;
  var carouselInner = document.getElementById("carousel-inner");
  var carouselIndicators = document.getElementById("carousel-indicators");
  var innerContent = "";
  var indicatorsContent = "";
  for (var i = 0; i < m.length; i++) {
    var c = "";
    if (i == 0) {
      c = " active";
    }
    innerContent += '<div class="item' + c + '"><img src="' + m[i].href + '"><div class="carousel-caption"></div>   </div>';
    indicatorsContent += '<li class=' + c + 'data-target="#carousel-example-generic" data-slide-to="' + i + '"></li>';
  }
  carouselInner.innerHTML = innerContent;
  carouselIndicators.innerHTML = indicatorsContent;
  var carouselExampleGeneric = document.getElementById("carousel-example-generic");
  //carouselExampleGeneric.carousel();
}

谢谢,为什么我们需要注释掉这一行代码 - carouselExampleGeneric.carousel(); - learner
我不确定你想用这段代码做什么,但是我认为它在我的环境中会抛出一个错误,所以我暂时将其注释掉以确保其他部分能够正常工作。 - wrobbler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接