gpt4 book ai didi

javascript - 复用ajax函数

转载 作者:行者123 更新时间:2023-11-28 20:40:16 24 4
gpt4 key购买 nike

我想重用这个函数,但我想每次都更改id(在本例中为品牌),我是否必须每次都重新创建该函数还是有其他方法?

这是函数:

        function showUser(str)
{
if (str=="")
{
document.getElementById("brand").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("brand").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","inc/form_rest.php?q="+str,true);
xmlhttp.send();
}

最佳答案

将id作为函数的参数传入?

关于javascript - 复用ajax函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14541133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com