将 2 个函数 JavaScript 合并为 1

Combine 2 function javascript into 1

本文关键字:合并 JavaScript 函数      更新时间:2023-09-26
函数显示角色(str,x(        {            if (str=="(              {              document.getElementById("txtHintrole"+x+"(.innerHTML=";              返回;              }            如果(窗口。XMLHttpRequest(              {//IE7+、Firefox、Chrome、Opera、Safari 的代码              xmlhttp=new XMLHttpRequest((;              }            还              {//IE6, IE5 的代码              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"(;              }            xmlhttp.onreadystatechange=function((              {              if (xmlhttp.readyState==4 && xmlhttp.status==200(                {                document.getElementById("txtHintrole"+x+"(.innerHTML=xmlhttp.responseText;                }              }            xmlhttp.open("GET","http://localhost/tes/index.php/form/role/"+str,true(;            xmlhttp.send((;        }
函数显示用户(str,x(        {            if (str=="(              {              document.getElementById("txtHint"+x+"(.innerHTML=";              返回;              }            如果(窗口。XMLHttpRequest(              {//IE7+、Firefox、Chrome、Opera、Safari 的代码              xmlhttp=new XMLHttpRequest((;              }            还              {//IE6, IE5 的代码              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"(;              }            xmlhttp.onreadystatechange=function((              {              if (xmlhttp.readyState==4 && xmlhttp.status==200(                {                document.getElementById("txtHint"+x+"(.innerHTML=xmlhttp.responseText;                }              }            xmlhttp.open("GET","http://localhost/tes/index.php/form/hint/"+str,true(;            xmlhttp.send((;        }

我可以将这 2 个函数合并为 1 吗因为我需要它们来显示数据而且我不知道如何使用此设置2函数

newcell.childNodes[0].setAttribute("onchange","showUser(this.value,"+xx+"(;"(;

当然,只需创建一个包装函数:

var showBoth = function(str, x) {
    showRole(str, x);
    showUser(str, x);
};