【问题标题】:How to simplify jquery code with variables?如何使用变量简化 jquery 代码?
【发布时间】:2014-11-23 15:13:58
【问题描述】:

我已经完成了这个 jQuery 代码来切换带有图像预览的浮动部分。图层始终相同,只有图像发生变化。我设法简化了 css 中的代码,为元素提供类。一切正常。但是我发现优化 jQuery 代码非常困难。我对 jQuery 和 Javascript 还很陌生,所以我不太确定该怎么做。代码工作正常,但它非常庞大,我想简化它。我相信我必须使用变量。但是怎么做?任何帮助将不胜感激:)

这是我的 jQuery 代码:

//first layer

$(document).ready(function() {
$('.toggleLayer').click(function(){
$('#FloatingLayer').fadeIn('fast');
});
});

$(document).ready(function() {
$('#FloatingLayer').click(function(){
$(this).fadeOut('fast');
});
});


//second layer

$(document).ready(function() {
$('.toggleLayer2').click(function(){
$('#FloatingLayer2').fadeIn('fast');
});
});

$(document).ready(function() {
$('#FloatingLayer2').click(function(){
$(this).fadeOut('fast');
});
});



// third layer

$(document).ready(function() {
$('.toggleLayer3').click(function(){
$('#FloatingLayer3').fadeIn('fast');
});
});

$(document).ready(function() {
$('#FloatingLayer3').click(function(){
$(this).fadeOut('fast');
});
});

这是我的html:

<div class="fourcol toggleLayer">   
     <img src="img1" class="center" width="100%">
  </div>

  <div class="fourcol toggleLayer2">   
     <img src="img2.jpg" class="center" width="100%">
  </div>

  <div class="fourcol toggleLayer3">   
     <img src="img3" class="center" width="100%" >
  </div>

浮动部分:

<section class="Floating" id="FloatingLayer">
     <img src="img1.jpg" class="floatimg"/>
   </section>

   <section class="Floating" id="FloatingLayer2">
     <img src="img2.jpg" class="floatimg"/>
   </section>

  <section class="Floating" id="FloatingLayer3">
     <img src="img3.jpg" class="floatimg"/>
   </section>

【问题讨论】:

  • “切换层”元素的标记在哪里?它们是按钮还是什么?
  • 你是对的!对不起。我已经编辑了问题并添加了它。
  • 好的,我将编辑我的答案以使用您的标记;相同的基本思想。

标签: javascript jquery variables toggle simplify


【解决方案1】:

完全不更改您的标记...

var $body = $(document.body);

$body.on('click', '[class~=toggleLayer]', function () {
    var layerNum = this.className.match(/toggleLayer(\d+)/)[1];

    $('#FloatingLayer' + layerNum).fadeIn('fast');
});

$body.on('click', '.Floating', function () {
    $(this).fadeOut('fast');
});

但是,这里的行为并不明确,最好修改您的标记并使用 Pointy 建议的自定义 data- 属性。

【讨论】:

    【解决方案2】:

    利用您可以向标记添加信息来指导 JavaScript 代码提供的行为这一事实。假设您的切换控件是按钮:

    <button class=toggle-layer data-target=FloatingLayer1>Layer 1</button>
    <button class=toggle-layer data-target=FloatingLayer2>Layer 2</button>
    <button class=toggle-layer data-target=FloatingLayer3>Layer 3</button>
    

    编辑 — 或使用您的实际标记:

      <div class="fourcol toggleLayer" data-target=FloatingLayer1>   
         <img src="img1" class="center" width="100%">
      </div>
    
      <div class="fourcol toggleLayer" data-target=FloatingLayer2>   
         <img src="img2.jpg" class="center" width="100%">
      </div>
    
      <div class="fourcol toggleLayer" data-target=FloatingLayer3>   
         <img src="img3" class="center" width="100%" >
      </div>
    

    您现在可以为所有这些编写一个简单的处理程序:

    $("body").on("click", ".toggleLayer", function() {
      var targetId = $(this).data("target");
    
      $("#" + targetId).fadeIn("fast");
    });
    

    同样,一个处理程序可以完成关闭所有层的工作:

    $("body").on("click", ".Floating", function() {
        $(this).fadeOut("fast");
    });
    

    如果您在这些示例中使用事件委托,则不必将代码放入“就绪”处理程序中。

    【讨论】:

    • 谢谢!但是,我一定做错了什么。我使用了该代码,但它停止工作:(
    • @Owly 好吧,我本可以犯一些愚蠢的错误;如果您发布遇到的错误,可能很容易修复。
    • 谢谢。它不会在控制台中给我任何错误。当我点击它时它什么也没做。什么都没发生。我将使用我现在获得的代码编辑我的问题,以便您看到。
    • @Owly 好的,我更新了答案以更接近您的课程。我不确定出了什么问题;如果你没有收到错误,我要做的第一件事是在事件处理程序中调用一些 console.log() 以查看那里发生的任何事情。
    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多