简体   繁体   English

如何合并重复的javascript函数?

[英]How can I consolidate duplicated javascript functions?

I have a site to allow someone to place food orders. 我有一个网站,允许某人下订单。 Images of potential ingredients (determined by a MySQL query) can be clicked to add or remove them, and the image will toggle on each click. 可以单击潜在成分的图像(由MySQL查询确定)以添加或删除它们,并且每次单击都会切换图像。

The problem I'm having is for each new item I am having to duplicate the function and just change the variable names for each new function. 我遇到的问题是对于每个新项目, 我都必须复制该函数,而只是更改每个新函数的变量名。 I'm sure there must be a way to simplify to dynamically apply to all of the ingredients without all of the redundant code. 我敢肯定,必须有一种方法可以简化以动态地应用到所有成分,而无需所有冗余代码。 Here is the code just for two. 这是仅两个的代码。 There are dozens. 有几十个。 Any suggestions would be appreciated. 任何建议,将不胜感激。

window.onload = function () {
    var ProductElement = document.getElementById('Ketchup');
    if (ProductElement != null) {
        Ketchupobj = document.getElementById('Ketchup')
        document.getElementById('Ketchuptogg').onclick = function () {
            Ketchuptoggle();
        }
    }

    var ProductElement = document.getElementById('Mustard');
    if (ProductElement != null) {
        Mustardobj = document.getElementById('Mustard')
        document.getElementById('Mustardtogg').onclick = function () {
            Mustardtoggle();
        }
    }
}

function Ketchuptoggle() {
    if (Ketchuptggle == 'on') {
        Ketchupobj.src = "Ketchup.jpg";
        Ketchuptggle = 'off';
    } else {
        Ketchupobj.src = "noKetchup.jpg";
        Ketchuptggle = 'on';
    }
}

function Mustardtoggle() {
    if (Mustardtggle == 'on') {
        Mustardobj.src = "Mustard.jpg";
        Mustardtggle = 'off';
    } else {
        Mustardobj.src = "noMustard.jpg";
        Mustardtggle = 'on';
    }
}


<table class="ing">
<tr>
<?php

for ($x=0; $x<5 AND $row = mysql_fetch_row($result);$x++ ) {
$caps=$row[1];
$caps=strtoupper($caps);
echo <<<image
<td><b>$caps</b><br>
<a id="$row[0]" class="toggle" href="#"><img id="$row[0]img" class="toggimg" 
src="$row[0].jpg" style="border-style: none" alt=""/></a>
</td>
image;
}
echo"</tr></table>";

Implicit this is your friend: 暗示这是你的朋友:

var toggles = document.getElementsByClassName('toggle');
for (var i=0; i<toggles.length; i++) {
  toggles[i].isOn = true;
  toggles[i].onclick = function(ev){
     var condiment = this.id;
     this.isOn = !this.isOn;
     document.getElementById(condiment+'img').src=(this.isOn?'':'no')+condiment+'.png';
   };
 }

With html you have the ability to add your property for an element, so you could do: 使用html,您可以为元素添加属性,因此可以执行以下操作:

<button class="btnProduct" data-type="mostard"> Mostard</button> 
<button class="btnProduct" data-type="ketchup"> Ketchup</button>
<button class="btnProduct" data-type="barbecue"> Barbecue</button> 

Then with a help of jquery you can do: 然后借助jquery,您可以执行以下操作:

$('btnProduct').click(function(){
    //So, here you could use a switch or some logic 
    // to do different things for data-type
    console.log($(this).attr('data-type'))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM