首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery更改.prop href

Jquery更改.prop href
EN

Stack Overflow用户
提问于 2016-10-28 02:18:15
回答 3查看 994关注 0票数 0

当单击#lienImg href时,我正在尝试更改.product_thumbnails li a,但似乎不起作用。奇怪的是,.primary_image更新了.

代码语言:javascript
复制
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
    $(".product_thumbnails li a").click(function(e) {
      var imglink = $(this).attr("href");
      return e.preventDefault(); 
      $("#lienImg").prop("href", imglink);
      $(".primary_image").attr("src", imglink); 
      $(".product_thumbnails li").removeClass("active");
      $(this).parent().addClass("active"); !1
    })
    });
  </script>
代码语言:javascript
复制
<div class="product_images">
  <a href="https://images.bigcartel.com/product_images/187353065/produit_08-294HR.jpg?auto=format&amp;fit=max&amp;w=1200" class="MagicZoom" rel="zoom-position: inner" id="lienImg" title="" style="position: relative; display: inline-block; text-decoration: none; outline: 0px; width: 576px;"><img src="https://images.bigcartel.com/product_images/187353089/produit_08-304HR.jpg?auto=format&amp;fit=max&amp;h=1000&amp;w=1000" alt="Image of Bol Or" class="primary_image" style="opacity: 1;"><div class="MagicZoomBigImageCont MagicBoxShadow" style="overflow: hidden; z-index: 100; top: -100000px; position: absolute; width: 576px; height: 384px; left: 0px; opacity: 0;"><div class="MagicZoomHeader" style="position: relative; z-index: 10; left: 0px; top: 0px; padding: 3px; display: none; visibility: hidden;"></div><div style="overflow: hidden;"><img src="https://images.bigcartel.com/product_images/187353065/produit_08-294HR.jpg?auto=format&amp;fit=max&amp;w=1200" style="padding: 0px; margin: 0px; border: 0px; width: auto; height: auto; position: relative; left: -350px; top: -413px;"></div><div style="color: rgb(255, 0, 0); font-size: 10px; font-weight: bold; font-family: Tahoma; position: absolute; width: 100%; display: none; left: 0px; top: 364px;">Please upgrade to full version of Magic Zoom™</div></div><div class="MagicZoomPup" style="z-index: 10; position: absolute; overflow: hidden; display: none; visibility: hidden; width: 276px; height: 184px; opacity: 0.5; left: 193px; top: 200px;"></div><div class="MagicZoomHint" style="display: block; overflow: hidden; position: absolute; visibility: visible; z-index: 1; left: 2px; right: auto; top: 2px; bottom: auto; opacity: 0.75; max-width: 572px;">Zoom</div></a>
  
    <ul class="product_thumbnails">
      
        <li class=""><a href="https://images.bigcartel.com/product_images/187353065/produit_08-294HR.jpg?auto=format&amp;fit=max&amp;h=1000&amp;w=1000"><img src="https://images.bigcartel.com/product_images/187353065/produit_08-294HR.jpg?auto=format&amp;fit=max&amp;w=300" alt="Image of Bol Or"></a></li>
      
        <li class="active"><a href="https://images.bigcartel.com/product_images/187353089/produit_08-304HR.jpg?auto=format&amp;fit=max&amp;h=1000&amp;w=1000"><img src="https://images.bigcartel.com/product_images/187353089/produit_08-304HR.jpg?auto=format&amp;fit=max&amp;w=300" alt="Image of Bol Or" data-pin-nopin="true"></a></li>
      
        <li><a href="https://images.bigcartel.com/product_images/187353110/produit_08-311HR.jpg?auto=format&amp;fit=max&amp;h=1000&amp;w=1000"><img src="https://images.bigcartel.com/product_images/187353110/produit_08-311HR.jpg?auto=format&amp;fit=max&amp;w=300" alt="Image of Bol Or" data-pin-nopin="true"></a></li>
      
    </ul>
  
</div>

EN

回答 3

Stack Overflow用户

发布于 2016-10-28 02:26:30

为什么在执行代码之前使用return in ?在控制台中调试,你会发现问题。像下面这样更新代码,它运行良好。

代码语言:javascript
复制
$(document).ready(function() {
        $(".product_thumbnails li a").click(function(e) {
          var imglink = $(this).attr("href");
           e.preventDefault(); 
          $("#lienImg").prop("href", imglink);
          $(".primary_image").attr("src", imglink); 
          $(".product_thumbnails li").removeClass("active");
          $(this).parent().addClass("active"); !1
        })
        });
票数 0
EN

Stack Overflow用户

发布于 2016-10-28 02:34:53

正如web开发人员所说的,preventDefault()函数不需要返回,您还需要指向div中的img。

代码语言:javascript
复制
$(document).ready(function() {
    $(".product_thumbnails li a").click(function(e) {
      e.preventDefault();
      var imglink = $(this).attr("href");
 	$("#lienImg > img").prop("src", imglink);
          // Notice you need to point to a image
    })
 });
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="product_images">
  <a href="https://images.bigcartel.com/product_images/187353065/produit_08-294HR.jpg?auto=format&amp;fit=max&amp;w=1200" class="MagicZoom" rel="zoom-position: inner" id="lienImg" title="" style="position: relative; display: inline-block; text-decoration: none; outline: 0px; width: 576px;"><img src="https://images.bigcartel.com/product_images/187353089/produit_08-304HR.jpg?auto=format&amp;fit=max&amp;h=1000&amp;w=1000" alt="Image of Bol Or" class="primary_image" style="opacity: 1;"><div class="MagicZoomBigImageCont MagicBoxShadow" style="overflow: hidden; z-index: 100; top: -100000px; position: absolute; width: 576px; height: 384px; left: 0px; opacity: 0;"><div class="MagicZoomHeader" style="position: relative; z-index: 10; left: 0px; top: 0px; padding: 3px; display: none; visibility: hidden;"></div><div style="overflow: hidden;"><img src="https://images.bigcartel.com/product_images/187353065/produit_08-294HR.jpg?auto=format&amp;fit=max&amp;w=1200" style="padding: 0px; margin: 0px; border: 0px; width: auto; height: auto; position: relative; left: -350px; top: -413px;"></div><div style="color: rgb(255, 0, 0); font-size: 10px; font-weight: bold; font-family: Tahoma; position: absolute; width: 100%; display: none; left: 0px; top: 364px;">Please upgrade to full version of Magic Zoom™</div></div><div class="MagicZoomPup" style="z-index: 10; position: absolute; overflow: hidden; display: none; visibility: hidden; width: 276px; height: 184px; opacity: 0.5; left: 193px; top: 200px;"></div><div class="MagicZoomHint" style="display: block; overflow: hidden; position: absolute; visibility: visible; z-index: 1; left: 2px; right: auto; top: 2px; bottom: auto; opacity: 0.75; max-width: 572px;">Zoom</div></a>
  
    <ul class="product_thumbnails">
      
        <li class=""><a href="https://images.bigcartel.com/product_images/187353065/produit_08-294HR.jpg?auto=format&amp;fit=max&amp;h=1000&amp;w=1000"><img src="https://images.bigcartel.com/product_images/187353065/produit_08-294HR.jpg?auto=format&amp;fit=max&amp;w=300" alt="Image of Bol Or"></a></li>
      
        <li class="active"><a href="https://images.bigcartel.com/product_images/187353089/produit_08-304HR.jpg?auto=format&amp;fit=max&amp;h=1000&amp;w=1000"><img src="https://images.bigcartel.com/product_images/187353089/produit_08-304HR.jpg?auto=format&amp;fit=max&amp;w=300" alt="Image of Bol Or" data-pin-nopin="true"></a></li>
      
        <li><a href="https://images.bigcartel.com/product_images/187353110/produit_08-311HR.jpg?auto=format&amp;fit=max&amp;h=1000&amp;w=1000"><img src="https://images.bigcartel.com/product_images/187353110/produit_08-311HR.jpg?auto=format&amp;fit=max&amp;w=300" alt="Image of Bol Or" data-pin-nopin="true"></a></li>
      
    </ul>
  
</div>

票数 0
EN

Stack Overflow用户

发布于 2016-10-28 08:28:28

删除返回的e.preventDefault();,因为它会在初始化var imglink之后立即破坏代码。

代码语言:javascript
复制
$(".product_thumbnails li a").click(function(e) {
    e.preventDefault();
    var imglink = $(this).prop("href");
    $("#lienImg").prop("href", imglink); 
    // ... extra code
});

代码中的这一行:

代码语言:javascript
复制
 $("#lienImg").prop("href", imglink); 

已经将lienImg的href属性更改为单击的product_thumbnails链接。

另一方面,由于下面这一行,.primary_image会被更新:

代码语言:javascript
复制
$(".primary_image").attr("src", imglink);

删除它不会使primary_ image的映像更改为单击的product_thumbnails映像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40291593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档