RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 606226
Accepted
Tariel
Tariel
Asked:2020-12-21 22:10:33 +0000 UTC2020-12-21 22:10:33 +0000 UTC 2020-12-21 22:10:33 +0000 UTC

如何有条件地更改js中元素的id

  • 772

下午好。

HTML文档包含一组缩略图图像。

<a href="javascript:void(0);"><img class="miniVert" src="images/do_prof.jpg" onclick="setBigImage(this);" oncontextmenu="return false;"></a>

通过单击每个,它会在元素中以更大的尺寸显示:

<div id="resimagewrapper">              
    <img id="bigCertVert" src="images/teach_ut.jpg" oncontextmenu="return false;" />
</div> 

使用功能:

function setBigImage(obj){
    document.getElementById('bigCertVert').src = obj.src;               
}

所有图片都是横向的,所以元素的尺寸#bigCertVert是硬编码的。
现在有纵向的图片,在这个方向上创建了一个输出元素,需要替换函数检查迷你图像的类,并根据它在所需元素中输出大尺寸。
需要一个纯js解决方案。提前致谢。

javascript
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Moonvvell
    2020-12-21T22:42:07Z2020-12-21T22:42:07Z

    function setBigImage(obj){
     if (obj.className == "miniVert"){
     document.getElementById('bigCertVert').src = obj.src; 
     } else {
     document.getElementById('bigCertHoriz').src = obj.src; 
     }                  
    }
    .miniVert, .miniHoriz{
      width: 200px;
    }
    <a href="javascript:void(0);"><img class="miniVert" src="http://images6.fanpop.com/image/photos/33400000/NEKO-k-33431290-714-714.png" onclick="setBigImage(this);" oncontextmenu="return false;"></a>
    
    <a href="javascript:void(0);"><img class="miniHoriz" src="http://images.boomsbeat.com/data/images/full/24384/cat_4-jpg.jpg" onclick="setBigImage(this);" oncontextmenu="return false;"></a>
    <div id="resimagewrapper">              
        <img id="bigCertVert" src="images/teach_ut.jpg" oncontextmenu="return false;" />
        <img id="bigCertHoriz" src="images/teach_ut.jpg" oncontextmenu="return false;" />
    </div> 

    例如像这样。

    • 1
  2. user176262
    2020-12-21T22:42:44Z2020-12-21T22:42:44Z
    function setBigImage(obj){
      if (obj.classList.contains("miniVert"))
        document.getElementById('bigCertVert').src = obj.src;               
      else
        document.getElementById('bigCertHoriz').src = obj.src;               
    }
    
    • 1
  3. Best Answer
    Yuri
    2020-12-21T22:44:01Z2020-12-21T22:44:01Z

    function setBigImage(obj){
      document.getElementById('bigCertVert').src = obj.src;
      if(obj.classList.contains('type0')){
        document.getElementById('bigCertVert').classList.add("type0");
        document.getElementById('bigCertVert').classList.remove("type1");
      }else if(obj.classList.contains('type1')){
        document.getElementById('bigCertVert').classList.add("type1");
        document.getElementById('bigCertVert').classList.remove("type0");
      };
    }
    #resimagewrapper img.type0 {width:100px;height:50px}
    #resimagewrapper img.type1 {width:50px;height:50px}
    <div class="list">
      <a href="javascript:void(0);"><img class="miniVert type0" src="https://www.gravatar.com/avatar/0089b585f2499f09a32d95c06d545ba0?s=32&d=identicon&r=PG&f=1" onclick="setBigImage(this);" oncontextmenu="return false;"></a>
      <a href="javascript:void(0);"><img class="miniVert type1" src="https://www.gravatar.com/avatar/0089b585f2499f09a32d95c06d545ba0?s=32&d=identicon&r=PG&f=1" onclick="setBigImage(this);" oncontextmenu="return false;"></a>
    </div>
    <div id="resimagewrapper">              
        <img id="bigCertVert" class="type0" src="images/teach_ut.jpg" oncontextmenu="return false;" />
    </div> 

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5