通过标签html中的照片列表更改

Change through list of photos in tab html

本文关键字:照片 列表 标签 html      更新时间:2023-09-26

嗨,我正试图用HTML, CSS和Javascript为我的网站编码,我不习惯这些语言。

我有"标签",是由css作为ul在html中创建的,我想把图片与Javascript链接的图像作为onclick,这样我就可以改变通过我点击他们的照片。

代码如下:

脚本函数

<script>
    function changeImage(){
        var image = document.getElementById('myImage');
        if(image.src.match("img/test/image1.jpg")){
            image.src = "img/test/image2.jpg";
        }
        else if(image.src.match("img/test/image2.jpg")){
            image.src = "img/test/image3.jpg";
        }
        else if(image.src.match("img/test/image3.jpg")){
            image.src = "img/test/image4.jpg";
        }
        else if(image.src.match("img/test/image4.jpg")){
            image.src = "img/test/image5.jpg";
        }
        else if(image.src.match("img/test/image5.jpg")){
            image.src = "img/test/image1.jpg";
        }
    }
</script>
<<p> HTML部分/strong>
 <div class="tabs">
 <ul class="tab-links">
       <li class="active"><a href="#tab1">Kitchenette</a></li>
          <li><a href="#tab2">Double Queen</a></li>
          <li><a href="#tab3">Single Queen</a></li>
          <li><a href="#tab4">Standard Room</a></li>
          <li><a href="#tab5">Partial View Room</a></li>
 </ul>
 <div class="tab-content">
     <div id="tab1" class="tab active">
        <table style="width:100%">
           <tr>
                <td>
                    <img src="img/test/image1.jpg" id="myImage" width="550" height="300" alt="Room Image" onclick="changeImage()" />
                </td>
            </tr>
           <tr>
            <td>
                 <p> Description of the room should be in here. </p>
            </td>
        </tr>
    </table>
</div>
<div id="tab2" class="tab">
    <table style="width:100%">
        <tr>
            <td>
                <img src="img/test/second/image1.jpg" id="myImage" width="550" height="300" alt="Room Image" onclick="changeImage2()" />
            </td>
        </tr>
        <tr>
            <td>
                <p> Description of the room should be in here. </p>
            </td>
        </tr>
    </table>
    </div>
CSS部分

/*----- Tabs -----*/
.tabs {
    width:100%;
    display:inline-block;
}
    /*----- Tab Links -----*/
    /* Clearfix */
    .tab-links:after {
        display:block;
        clear:both;
        content:'';
    }
    .tab-links li {
        margin:0px 2px;
        float:left;
        list-style:none;
    }
        .tab-links a {
            /*padding:5px 5px;*/
            display:inline-block;
            border-radius:3px 3px 0px 0px;
            background:#7FB5DA;
            font-size:14px;
            font-weight:600;
            color:#4c4c4c;
            transition:all linear 0.15s;
        }
        .tab-links a:hover {
            background:#a7cce5;
            text-decoration:none;
        }
    li.active a, li.active a:hover {
        background:#fff;
        color:#4c4c4c;
    }
    /*----- Content of Tabs -----*/
    .tab-content {
        /*padding:15px;*/
        border-radius:4px;
        box-shadow:-1px 1px 1px rgba(0,0,0,0.15);
        background:#fff;
    }
        .tab {
            display:none;
        }
        .tab.active {
            display:block;
        }

我遇到的问题是第一个选项卡工作得很好但是如果我转到第二个选项卡照片不会改变但是第一个选项卡的照片会改变

所以我认为onclick读取第二个选项卡的点击并将其应用于第一个选项卡但我不确定这个。

请帮帮我…我被困住了

将您的图像放在选项卡中,并将脚本更改为如下所示:

$(document).ready(function() {
    $('.tabs .tab-links a').on('click', function(e)  {
        var currentAttrValue = jQuery(this).attr('href');
 
        // Show/Hide Tabs
        $('.tabs ' + currentAttrValue).show().siblings().hide();
 
        // Change/remove current tab to active
        $(this).parent('li').addClass('active').siblings().removeClass('active');
 
        e.preventDefault();
    });
});
/*----- Tabs -----*/
.tabs {
    width:100%;
    display:inline-block;
}
 
    /*----- Tab Links -----*/
    /* Clearfix */
    .tab-links:after {
        display:block;
        clear:both;
        content:'';
    }
 
    .tab-links li {
        margin:0px 5px;
        float:left;
        list-style:none;
    }
 
        .tab-links a {
            padding:9px 15px;
            display:inline-block;
            border-radius:3px 3px 0px 0px;
            background:#7FB5DA;
            font-size:16px;
            font-weight:600;
            color:#4c4c4c;
            transition:all linear 0.15s;
        }
 
        .tab-links a:hover {
            background:#a7cce5;
            text-decoration:none;
        }
 
    li.active a, li.active a:hover {
        background:#fff;
        color:#4c4c4c;
    }
 
    /*----- Content of Tabs -----*/
    .tab-content {
        padding:15px;
        border-radius:3px;
        box-shadow:-1px 1px 1px rgba(0,0,0,0.15);
        background:#fff;
    }
 
        .tab {
            display:none;
        }
 
        .tab.active {
            display:block;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tabs">
    <ul class="tab-links">
       <li class="active"><a href="#tab1">Kitchenette</a></li>
          <li><a href="#tab2">Double Queen</a></li>
          <li><a href="#tab3">Single Queen</a></li>
          <li><a href="#tab4">Standard Room</a></li>
          <li><a href="#tab5">Partial View Room</a></li>
    </ul>
 
    <div class="tab-content">
        <div id="tab1" class="tab active">
            <img src="/path/to/img1.jpg" alt="Kitchenette" />
            <p>Description</p>
        </div>
 
        <div id="tab2" class="tab">
            <img src="/path/to/img2.jpg" alt="Double Queen" />
            <p>Description</p>
        </div>
 
        <div id="tab3" class="tab">
            <img src="/path/to/img3.jpg" alt="Single Queen" />
            <p>Description</p>
        </div>
 
        <div id="tab4" class="tab">
            <img src="/path/to/img4.jpg" alt="Standard Room" />
            <p>Description</p>
        </div>
        <div id="tab5" class="tab">
            <img src="/path/to/img5.jpg" alt="Partial View Room" />
            <p>Description</p>
        </div>
    </div>
</div>

读取元素标识符:id和class属性,对id和class有一个了解。

正如它所说,

此名称在文档中必须是唯一的。

你不能让多个元素共享一个id。如果这样做,getElementById可能只得到第一个元素。

解决这个问题的方法之一是:为每个id使用不同的id(例如:myImageTab1 myImageTab2)。如果你想修改样式,可以给所有的图像元素添加一个类。

你不需要javascript,你可以用一个简单的css鼠标悬停。

#tabs {
  border: 1px solid #DEDEDE;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  height: 365px;
  position: relative;
  overflow: hidden;
}
.tabs-content {
  padding: 25px;
  width: 100%;
  height: 300px;
  overflow: hidden;
  position: absolute;
  bottom: 0;
  left: 0;
  display: none;
}
.tabs {
  overflow: hidden;
  background: #e1e1e1;
  background: -moz-linear-gradient(center top, #f2f2f2, #e1e1e1);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #f2f2f2), color-stop(100%, #e1e1e1));
  -moz-border-radius: 4px 4px 0 0;
  -webkit-border-radius: 4px 4px 0 0;
  border-radius: 4px 4px 0 0;
  -webkit-box-shadow: 0 1px 0 #FFF inset;
  -moz-box-shadow: 0 1px 0 #FFF inset;
  box-shadow: 0 1px 0 #FFF inset;
}
.tabs a {
  display: block;
  float: left;
  font: 15px/35px Arial, Helvetica, Sans-serif;
  padding: 0 20px 0 40px;
  color: #999;
  text-shadow: 0 1px 0 #FFF;
  border-left: solid 1px rgba(0, 0, 0, 0.05);
  border-right: solid 1px rgba(255, 255, 255, 0.7);
  position: relative;
  overflow: hidden;
}
.tabs a:after {
  content: '✔';
  position: absolute;
  top: 0px;
  left: 10px;
  line-height: 21px;
  font-size: 10px;
  width: 21px;
  text-align: center;
  margin: 7px 10px 5px 0;
  background: #000;
  font-size: 12px;
  -moz-border-radius: 21px;
  -webkit-border-radius: 21px;
  border-radius: 21px;
  background: #bdbdbd;
  background: -moz-linear-gradient(center top, #d4d4d4, #bdbdbd);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #d4d4d4), color-stop(100%, #bdbdbd));
  -webkit-box-shadow: 0 1px 0 0 #FFF, 0 1px 0 0 rgba(0, 0, 0, 0.25) inset;
  -moz-box-shadow: 0 1px 0 0 #FFF, 0 1px 0 0 rgba(0, 0, 0, 0.25) inset;
  box-shadow: 0 1px 0 0 #FFF, 0 1px 0 0 rgba(0, 0, 0, 0.25) inset;
  text-shadow: 0 1px 0 #999;
  color: #ffffff;
}
.tabs a:hover {
  background: #FFF;
  border-left-color: #CCC;
}
.tabs a:hover:after {
  background: #038bd5;
  background: -moz-linear-gradient(center top, #2dc3fc, #038bd5);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #2dc3fc), color-stop(100%, #038bd5));
  text-shadow: 0 1px 0 #096c9e;
  -webkit-box-shadow: 0 1px 0 0 rgba(255, 255, 255, 0.45), 0 1px 0 0 rgba(0, 0, 0, 0.25) inset, 0 0 5px 0 rgba(0, 148, 255, 0.85);
  -moz-box-shadow: 0 1px 0 0 rgba(255, 255, 255, 0.45), 0 1px 0 0 rgba(0, 0, 0, 0.25) inset, 0 0 5px 0 rgba(0, 148, 255, 0.85);
  box-shadow: 0 1px 0 0 rgba(255, 255, 255, 0.45), 0 1px 0 0 rgba(0, 0, 0, 0.25) inset, 0 0 5px 0 rgba(0, 148, 255, 0.85)
}
.tabs a:hover + .tabs-content {
  display: block;
}
.tabs a:first-child {
  border-left-width: 0;
  display: block;
}
.tabs a:last-child {
  border-right-width: 0;
}
.tabs-content:hover {
  display: block;
}
img{width:550px;
    height:300px;}
<div id="content" role="main">
  <div id="tabs">
    <nav class="tabs">
      <a href="#">Kitchenette</a>
      <section class="tabs-content">
        <img src="http://www.rachelgallen.com/images/daisies.jpg" onmouseover="this.src='http://www.rachelgallen.com/images/snowdrops.jpg'" onmouseout="this.src='http://www.rachelgallen.com/images/daisies.jpg';" </img>
      </section>
      <a href="#">Double Queen</a>
      <section class="tabs-content">
        <img src="
                       http://www.rachelgallen.com/images/snowdrops.jpg" onmouseover="this.src='http://www.rachelgallen.com/images/yellowflowers.jpg';" onmouseout="this.src='http://www.rachelgallen.com/images/snowdrops.jpg';" </img>
      </section>
      <a href="#">Single Queen</a>
      <section class="tabs-content">
        <img src="
                       http://www.rachelgallen.com/images/mountains.jpg" onmouseover="this.src='http://www.rachelgallen.com/images/purpleflowers.jpg';" onmouseout="this.src='http://www.rachelgallen.com/images/mountains.jpg';" </img>
      </section>
      <a href="#">Full View</a>
      <section class="tabs-content">
        <img src="
                       http://www.rachelgallen.com/images/trees.jpg" onmouseover="this.src='http://www.rachelgallen.com/images/daisies.jpg';" onmouseout="this.src='http://www.rachelgallen.com/images/trees.jpg';" </img>
      </section><a href="#">Partial View</a>
      <section class="tabs-content">
        <img src="
                       http://www.rachelgallen.com/images/autumntrees.jpg" onmouseover="this.src='http://www.rachelgallen.com/images/trees.jpg';" onmouseout="this.src='http://www.rachelgallen.com/images/autumntrees.jpg';" </img>
      </section>
    </nav>
  </div>
</div>

您可以使用几行j查询来完成此操作。

$('#list1 li').click(function() {
  var j = $(this).data("type");
  $('#myimg').attr('src', j);
});
li {
  display: inline;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="list1">
  <li data-type="https://cdn.sstatic.net/stackoverflow/img/logo-10m.svg?v=fc0904eba1b1">Image 1</li>
  <li data-type="https://www.google.co.in/images/srpr/logo11w.png">Image 2</li>
  <li data-type="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-300mmf_35-56g_ed_vr/img/sample/sample4_l.jpg">Image 2</li>
</ul>
<div class="imagecontainer">
  <img id="myimg" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-300mmf_35-56g_ed_vr/img/sample/sample4_l.jpg" alt="" />
</div>