2017-06-18 88 views
0

我正在創建一個快速小頁面。我只是學習JavaScript,它正在採取一些習慣。我能夠創建一個頁面,單擊圖像時更改引號。現在,我希望圖像可以隨引號一起更改。所以它將是:點擊圖片1,圖片2將出現引用2.點擊圖片2,圖片3將出現引用3.更改圖片和點擊報價?

這是一個非常簡單的任務,但我沒有能夠找一個簡單的解決方案我已經花了幾個小時,這有點令人沮喪。

這裏的頁面:https://aprilehrlich.github.io/dataint/

下面是我在HTML已經有了:

<!DOCTYPE html> 
<html> 
<head> 
<title>Ida B. Wells</title> 
<link rel="stylesheet" type="text/css" href="styles.css"> 
<script type="text/javascript" src="jquery-3.2.1.min.js"></script> 
<script type="text/javascript" src="script.js"></script> 
</head> 
<body> 
<img id="MainProductImage"" src="https://i.ytimg.com/vi/pUMKFP4p2Lo/maxresdefault.jpg" width="500px"" id="myImg"> 
<h1>「Quote 1.」</h1> 
<p>- Ida B. Wells</p> 
</body> 
</html> 

的JavaScript:

$(document).ready(function() { 
var quote1 = "「Quote 1.」"; 
var quote2 = "「Quote 2」"; 
var quote3 = "「Quote 3」"; 
var quote4 = "「Quote 4」"; 
var quote5 = "「Quote 5」"; 
var quote6 = "「Quote 6」"; 
var quote7 = "「Quote 7」"; 
var quote8 = "「Quote 8」"; 
var quote9 = "「Quote 9」"; 
var quote10 = "「Quote 10」"; 
$("img").click(function() { 
    var currentQuote = $("h1").text(); 
    $("h1").fadeOut(0); 
    if (currentQuote == quote1) { 
     $("h1").text(quote2); 
    } 
    if (currentQuote == quote2) { 
     $("h1").text(quote3); 
    } 
    if (currentQuote == quote3) { 
     $("h1").text(quote4); 
    } 
    if (currentQuote == quote4) { 
     $("h1").text(quote5); 
    } 
    if (currentQuote == quote5) { 
     $("h1").text(quote6); 
    } 
    if (currentQuote == quote6) { 
     $("h1").text(quote7); 
    } 
    if (currentQuote == quote7) { 
     $("h1").text(quote8); 
    } 
    if (currentQuote == quote8) { 
     $("h1").text(quote9); 
    } 
    if (currentQuote == quote9) { 
     $("h1").text(quote10); 
    } 
    $("h1").fadeIn(1000); 
}); 
}) 

回答

1

你必須使用attr(屬性)函數來更改src元素。這是一個例子。我做你的代碼一點點動態的,所以你可以設置爲你希望儘可能多的報價,而無需更改代碼:

HTML:

<img id="MainProductImage" src="https://s-media-cache-ak0.pinimg.com/564x/2a/b4/e7/2ab4e74ad637f9c8ecb792b8b7d605a6.jpg" id="myImg" data-current-quote="0"> 
<h1>「Quote 1.」</h1> 
<p>- Ida B. Wells</p> 

的Javascript:

$(document).ready(function(){ 
    //Quotes/Image Array 
    var quotes = [ 
     { quote: "Quote1", img: "https://s-media-cache-ak0.pinimg.com/564x/2a/b4/e7/2ab4e74ad637f9c8ecb792b8b7d605a6.jpg"}, 
     { quote: "Quote2", img: "https://s-media-cache-ak0.pinimg.com/564x/af/52/03/af5203d09a0be9c9e655786c88c1d8b7.jpg"}, 
     { quote: "Quote3", img: "https://s-media-cache-ak0.pinimg.com/564x/04/37/3c/04373c4f98797b202d13b9882e137690.jpg"} 
    ]; 

    $("img").click(function(){ 

     var img = $("#MainProductImage"), 
      //We look for the next quote, if it's the last we go to the beginning 
      currentQuote = img.data("current-quote") === quotes.length -1 ? 0 : img.data("current-quote") + 1, 
      nextQuote = quotes[currentQuote]; 

     //Set the current quote index in a data attribute 
     img.data("current-quote", currentQuote); 
     $("h1").fadeOut(0); 
     //Change the text 
     $("h1").text(nextQuote.quote); 
     //Change the image 
     img.attr("src", nextQuote.img); 
    }); 
}); 

小提琴:https://jsfiddle.net/p2bus29y/2/

0
var data = [ 
    { 
     "CategoryID": 1, 
     "CategoryName": "Soler Power Plant", 
     "CategoryProducts": [ 
     { 
      "ID": 1, 
      "Name": 'Commercial Solar Power Plant', 
      "SubTitle": 'Eco, Eco Friendly, Energy, Green, Solar', 
      "Brand": 'ACS', 
      "Usage": '', 
      "Type": '', 
      "Price": 'Rs 5 Lakh/Unit', 
      "Body_Material": '', 
      "Description": 'Having a pre-determined quality administration system, we are thoroughly involved in delivering Commercial Solar Power Plant.', 
      "ImageUrl": 'assets/images/Products/Sola-power-plant.jpg', 
     }, 
     { 
      "ID": 2, 
      "Name": 'Industrial Solar Power Plants', 
      "SubTitle": 'Eco Friendly, Save Energy', 
      "Brand": 'ACS', 
      "Usage": '', 
      "Type": '', 
      "Price": 'Rs 5 Lakh/Unit', 
      "Body_Material": '', 
      "Description": 'So as to become a preferential business name, we are thoroughly engrossed in shipping an inclusive collection of Industrial Solar Power Plants.', 
      "ImageUrl": 'assets/images/Products/Industrial_Solar_Power_Plants.jpg', 
     }, 
     { 
      "ID": 3, 
      "Name": 'On Grid Solar Plant', 
      "SubTitle": 'Eco Friendly, Save Energy', 
      "Brand": 'ACS', 
      "Usage": '', 
      "Type": '', 
      "Price": 'Rs 1.1 Lakh/Unit', 
      "Body_Material": '', 
      "Description": "We are the leading firm of On Grid Solar Plant. To sustain the quality, our products are made under the guidance of industry certified professionals.", 
      "ImageUrl": 'assets/images/Products/On_Grid_Solar_Plant.jpg', 
     }, 
      { 
       "ID": 4, 
       "Name": 'On Grid Solar Power Plant', 
       "SubTitle": 'Eco Friendly, Save Energy', 
       "Brand": 'ACS', 
       "Usage": '', 
       "Type": '', 
       "Price": 'Rs 5 Lakh/Unit', 
       "Body_Material": '', 
       "Description": "We are the leading firm of On Grid Solar Power Plant. To sustain the quality, our products are made under the guidance of industry top professionals.", 
       "ImageUrl": 'assets/images/Products/On_Grid_Solar_Power_Plant.jpg', 
      }, 
      { 
       "ID": 5, 
       "Name": 'Solar Power Plant', 
       "SubTitle": 'Eco Friendly, Save Energy', 
       "Brand": 'ACS', 
       "Usage": '', 
       "Type": '', 
       "Price": 'Rs 5 Lakh/Unit', 
       "Body_Material": '', 
       "Description": "We are the leading firm of Solar Power Plant. To sustain the quality, our products are made under the guidance of industry certified professionals.", 
       "ImageUrl": 'assets/images/Products/Solar_Power_Plant.jpg', 
      }, 
     ] 
    } 
] 




function GetProducts() { 

      var products = data; 

      $.each(products, function() { 

       //var product = products.filter(filterByID) 
       var product = this; 
       $('#ProductDetails').append('<h2 class="text-center">' + this.CategoryName + '</h2>'); 
       var details = product; 
       $.each(details.CategoryProducts, function() { 


        tempData = tempData + '<div class="col-md-4 col-sm-6">' + 
            '<div class="project-box">' + 
             '<div class="frame-outer">' + 
              '<div class="frame">' + 
               '<img style="width:350px;" src="' + this.ImageUrl + '" class="attachment-340x220 size-340x220 wp-post-image" alt="">' + 
              '</div>' + 
             '</div>' + 
             '<div class="text-box">' + 
              '<h3><a>' + this.Name + '</a></h3>' + 
              '<div class="tags-row">' + 
               '<a class="link">' + this.SubTitle + '</a>' + 
              '</div>' + 
              '<p>' + this.Description + '</p>' + 
              //'<a href="javascript:ViewMore(' + this.ID + ')" class="btn-readmore">More Details</a>' + 
             '</div>' + 
            '</div>' + 
           '</div>'; 
       }); 

       $('#ProductDetails').append('<div class="row">' + tempData + '</div>'); 
       tempData = ''; 

      }); 
     } 

在這裏,這是我自己的代碼可能它可以幫助你。它不屬於你的代碼,但你可以理解,也許你需要這樣做

0

我用報價和圖片來源創建了一個數組。我增加了一個計數器來定義數組的索引。我也做到了,一旦所有事情都完成了,報價就會消失,所以文字在淡出之前不會改變。

$(document).ready(function(){ 
 
    // set array of quotes and images 
 
    var content = [ 
 
    ["「Quote 1.」", "https://i.ytimg.com/vi/pUMKFP4p2Lo/maxresdefault.jpg"], 
 
    ["「Quote 2.」", "http://www.stevensegallery.com/460/300"], 
 
    ["「Quote 3.」", "http://www.stevensegallery.com/300/200"], 
 
    ["「Quote 4.」", "http://www.stevensegallery.com/460/300"], 
 
    ["「Quote 5.」", "http://www.stevensegallery.com/300/200"], 
 
    ["「Quote 6.」", "http://www.stevensegallery.com/284/196"], 
 
    ["「Quote 7.」", "http://www.stevensegallery.com/300/200"], 
 
    ["「Quote 8.」", "http://www.stevensegallery.com/460/300"], 
 
    ["「Quote 9.」", "http://www.stevensegallery.com/300/200"], 
 
    ["「Quote 10.」", "http://www.stevensegallery.com/284/196"], 
 
    ]; 
 
    // set counter to 1 
 
    var count = 1; 
 
    $("img").click(function(){ 
 
    // check is end of array reached and start from beginning 
 
    count == content.length ? count = 0 : ""; 
 
    // fade h1 out 
 
    $("h1").fadeOut(500, function() { 
 
     // use counter to get the correct index of array to change the h1 to the relevent quote 
 
     $("h1").text(content[count][0]); 
 
     // use counter to get the correct index of array to change the h1 to the relevent image source 
 
     $("img").attr("src", content[count][1]); 
 
     // fade h1 back in after everything else is done 
 
     $("h1").fadeIn(500); 
 
     // increment count by 1 
 
     count++; 
 
    }); 
 
    }); 
 
});
<img id="MainProductImage"" src="https://i.ytimg.com/vi/pUMKFP4p2Lo/maxresdefault.jpg" width="500px"" id="myImg"> 
 
<h1>「Quote 1.」</h1> 
 
<p>- Ida B. Wells</p> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>