如何将多个javascript功能组合成一个?

时间:2015-07-25 16:54:18

标签: javascript jquery

我想知道如何将这4个javascript函数合并为一个。它们完全相同,只是针对第8行的不同ID。它用于Google adwords来电跟踪功能。

<script type="text/javascript">
        var callback = function(formatted_number, mobile_number) {
          // formatted_number: number to display, in the same format as
          //        the number passed to _googWcmGet().
          //        (in this case, '1-800-123-4567')
          // mobile_number: number formatted for use in a clickable link
          //        with tel:-URI (in this case, '+18001234567')
          var e = document.getElementById("number_link_one");
          e.href = "tel:" + mobile_number;
          e.innerHTML = ""
          e.appendChild(document.createTextNode(formatted_number));
        };
      </script>

<script type="text/javascript">
        var callback = function(formatted_number, mobile_number) {
          // formatted_number: number to display, in the same format as
          //        the number passed to _googWcmGet().
          //        (in this case, '1-800-123-4567')
          // mobile_number: number formatted for use in a clickable link
          //        with tel:-URI (in this case, '+18001234567')
          var e = document.getElementById("number_link_two");
          e.href = "tel:" + mobile_number;
          e.innerHTML = ""
          e.appendChild(document.createTextNode(formatted_number));
        };
      </script>

<script type="text/javascript">
        var callback = function(formatted_number, mobile_number) {
          // formatted_number: number to display, in the same format as
          //        the number passed to _googWcmGet().
          //        (in this case, '1-800-123-4567')
          // mobile_number: number formatted for use in a clickable link
          //        with tel:-URI (in this case, '+18001234567')
          var e = document.getElementById("number_link_three");
          e.href = "tel:" + mobile_number;
          e.innerHTML = ""
          e.appendChild(document.createTextNode(formatted_number));
        };
      </script>

<script type="text/javascript">
        var callback = function(formatted_number, mobile_number) {
          // formatted_number: number to display, in the same format as
          //        the number passed to _googWcmGet().
          //        (in this case, '1-800-123-4567')
          // mobile_number: number formatted for use in a clickable link
          //        with tel:-URI (in this case, '+18001234567')
          var e = document.getElementById("number_link_four");
          e.href = "tel:" + mobile_number;
          e.innerHTML = ""
          e.appendChild(document.createTextNode(formatted_number));
        };
      </script>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用数组迭代所需的字符串:

var callback = function(formatted_number, mobile_number, number) {
      listofNumbers = ["one","two","three","four"]
      for(i=0;i<listOfNumbers.length;i++){
          var e = document.getElementById("number_link_"+listOfNumbers[i]);
          e.href = "tel:" + mobile_number;
          e.innerHTML = ""
          e.appendChild(document.createTextNode(formatted_number));
      }
};

或者,如果在不同时间调用callback,您也可以 添加一个新参数以指定第8行使用的ID:

var callback = function(formatted_number, mobile_number, number) {
      // formatted_number: number to display, in the same format as
      //        the number passed to _googWcmGet().
      //        (in this case, '1-800-123-4567')
      // mobile_number: number formatted for use in a clickable link
      //        with tel:-URI (in this case, '+18001234567')
      var e = document.getElementById("number_link_"+which);
      e.href = "tel:" + mobile_number;
      e.innerHTML = ""
      e.appendChild(document.createTextNode(formatted_number));
};

然后,在将来的函数调用中,只需使用:

callback(formatted_number, mobile_number, "one"); //or "two", "three", etc.