如何在jquery中使用html函数和返回标记缩短重复代码

如何在jquery中使用html函数和返回标记缩短重复代码,jquery,repeat,minify,Jquery,Repeat,Minify,我只是在学习如何缩短重复代码。也许有人能告诉我在哪里可以学到更多关于这方面的知识 我正在尝试缩短以下代码。我使用var-dph设法缩短了时间 例如:如何缩短以下命令: html(函数(){ 返回$(this.html()。替换( 我试图缩小/缩小的代码: setTimeout( function() { var dph = $("#description-paste-here"); dph.html(function () { r

我只是在学习如何缩短重复代码。也许有人能告诉我在哪里可以学到更多关于这方面的知识

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

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

html(函数(){

返回$(this.html()。替换(

我试图缩小/缩小的代码:

  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);
setTimeout(
函数()
{
var dph=$(“#说明粘贴在此处”);
html(函数(){
return$(this.html().replace(“请查看估价以了解更多信息。”,“此项目附带估价。”,“有关此项目或您可能见过的任何其他项目的更多信息,请随时向我发送电子邮件,我将尽快回复。”,“”);
});
html(函数(){
return$(this.html()。replace(“

有关此项目或您可能看到的任何其他项目的详细信息,请随时向我发送电子邮件,我将尽快回复。”,“”); }); html(函数(){ var findSHDL=$('#thestoreshdl').html(); 返回$(this.html().replace(“00002340”,findSHDL); }); html(函数(){ var findStore=$(“#存储位置”).html(); return$(this.html().replace(“东维多利亚公园”,findStore); }); html(函数(){ var findHours=$('#thestorehours').html(); return$(this.html().replace(“周一至周六上午9:30–下午5点”,findHours); }); }, 100);

非常感谢!:-)

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

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

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

let replacements=[{
target:“请查看估价了解更多信息。此物品附带估价。有关此物品或任何其他物品的更多信息,请随时向我发送电子邮件,我将尽快回复。”,
替换:“”
}, {
目标:“

有关此项目或您可能看到的任何其他项目的更多信息,请随时向我发送电子邮件,我将尽快回复。”, 替换:“” }, { 目标:“00002340”, 替换:$('#thestoreshdl').html() }, { 目标:"东维多利亚公园",, 替换:$(“#存储位置”).html() }, { 目标:“周一至周六上午9:30–下午5点”, 替换:$('thestorehours').html() }]; 设置超时(()=>{ $(“#说明粘贴在此处”).html((i,h)=>{ forEach(o=>h=h.replace(o.target,o.replacement)); 返回h; }); },100);
。隐藏{
显示:无;
}

请查看估价了解更多信息。此物品附带估价。有关此物品或您可能见过的任何其他物品的更多信息,请随时向我发送电子邮件,我将尽快回复。


有关此项目或您可能见过的任何其他项目的更多信息,请随时向我发送电子邮件,我将尽快回复。

00002340

东维多利亚公园

周一至周六上午9:30–下午5点 存储HDL。。。 地点。。。
商店营业时间…
非常感谢@Rory McCrossan。工作完全符合预期,而且比我想要的更干净。Champion!:-)