【问题标题】:How to shorten repeating code with html function and return tags in jquery如何使用html函数缩短重复代码并在jquery中返回标签
【发布时间】:2020-11-21 13:09:19
【问题描述】:

我只是在学习如何缩短重复代码。也许有人可以指出我可以在哪里了解更多信息?

我正在尝试缩短以下代码。我设法使用 var dph 缩短。

例如:如何缩短以下命令:

html(函数() {

返回 $(this).html().replace(

我要缩小/缩小的代码:

  setTimeout(
function() 
{
      var dph = $("#description-paste-here");
      dph.html(function () {
        return $(this).html().replace("Please see the valuation for more information.", "This item comes with a valuation.", "For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.", "");
      });
      dph.html(function () {
        return $(this).html().replace("<br><br>For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.", "");
      });
      dph.html(function () {
        var findSHDL = $('#thestoreshdl').html();
        return $(this).html().replace("00002340", findSHDL);
      });
      dph.html(function () {
        var findStore = $('#thestorelocation').html();
        return $(this).html().replace("East Victoria Park", findStore);
      });
      dph.html(function () {
        var findHours = $('#thestorehours').html();
        return $(this).html().replace("9:30am – 5pm Monday to Saturday", findHours);
      });
}, 100);

非常感谢! :-)

【问题讨论】:

    标签: jquery repeat minify


    【解决方案1】:

    首先请注意replace() 只接受两个参数,即要搜索的字符串和要替换它的字符串。第一个示例提供了多个将被忽略的参数。我想这只是一个错字。

    关于您的问题,实现目标的最简单方法是将 search.replace 值保存在可以迭代的对象数组中。

    此外,当您在每个函数中处理相同元素的 HTML 时,您可以将它们全部加入到单个操作中。试试这个:

    let replacements = [{
      target: 'Please see the valuation for more information. This item comes with a valuation. For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.',
      replacement: ''
    }, {
      target: '<br><br>For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.',
      replacement: ''
    }, {
      target: '00002340',
      replacement: $('#thestoreshdl').html()
    }, {
      target: 'East Victoria Park',
      replacement: $('#thestorelocation').html()
    }, {
      target: '9:30am – 5pm Monday to Saturday',
      replacement: $('#thestorehours').html()
    }];
    
    setTimeout(() => {
      $("#description-paste-here").html((i, h) => {
        replacements.forEach(o => h = h.replace(o.target, o.replacement));
        return h;
      });
    }, 100);
    .hidden {
      display: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="description-paste-here">
      Please see the valuation for more information. This item comes with a valuation. For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.
      <br><br>For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.
      <br /><br /> 00002340
      <br /><br /> East Victoria Park
      <br /><br /> 9:30am – 5pm Monday to Saturday
    </div>
    
    <div class="hidden" id="thestoreshdl">The store HDL...</div>
    <div class="hidden" id="thestorelocation">The location...</div>
    <div class="hidden" id="thestorehours">The store hours...</div>

    【讨论】:

    • 非常感谢@Rory McCrossan。完全按预期工作,并且比我想要的更干净。冠军! :-)
    猜你喜欢
    • 2020-10-09
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多