Javascript “创建”;分区;使用document.createElement(“div”);在每次通话中倍增
我创建了一个函数“getInfoProduits()”,其作用是获取产品信息并显示它们。为此,它使用“document.createElement(“div”);”创建“div”,其中包含产品信息。但问题是,每次我单击调用此函数的按钮时,这些div都会将每次调用此函数的次数相乘。这完全合乎逻辑,但我还没有解决办法。 这是第四个功能:Javascript “创建”;分区;使用document.createElement(“div”);在每次通话中倍增,javascript,createelement,Javascript,Createelement,我创建了一个函数“getInfoProduits()”,其作用是获取产品信息并显示它们。为此,它使用“document.createElement(“div”);”创建“div”,其中包含产品信息。但问题是,每次我单击调用此函数的按钮时,这些div都会将每次调用此函数的次数相乘。这完全合乎逻辑,但我还没有解决办法。 这是第四个功能: function getInfoProduits() { var request = new XMLHttpRequest(); if(i<i
function getInfoProduits()
{
var request = new XMLHttpRequest();
if(i<idProduits.length)
{
request.open("GET","http://patisserie-orient.fr/prestashop/prestashop/api/products/"+idProduits[i]+"?PHP_AUTH_USER="+PHP_AUTH_USER+"&ws_key="+ws_key,true);
request.onreadystatechange = function()
{
if(request.readyState==4)
{
//alert("Status2 is "+request.status);
if (request.status == 200 || request.status == 0)
{
response1 = request.responseXML.documentElement;
nameProduits[i] = response1.getElementsByTagName('name')[0].getElementsByTagName('language')[0].firstChild.data;
priceProduits[i] = response1.getElementsByTagName('price')[0].firstChild.data+" €";
descriptionProduits[i] = response1.getElementsByTagName('description_short')[0].getElementsByTagName('language')[0].firstChild.data;
quantitieProduitsDisponible[i] = response1.getElementsByTagName('quantity')[0].firstChild.data;
idImageProduits[i] = response1.getElementsByTagName('image')[0].getElementsByTagName('id')[0].firstChild.data;
maDiv = document.createElement("div");
maDiv.id = 'id_de_la_div'+i;
malabel = document.createElement("div");
malabel1 = document.createElement("div");
malabel.innerHTML=nameProduits[i];
malabel1.innerHTML=priceProduits[i];
maDiv.innerHTML='<img src='+url+' width="50" height="50" align="right">';
maDiv.className="ui-bar ui-bar-e cadre";
document.getElementById("divbar").appendChild(maDiv);
document.getElementById('id_de_la_div'+i).appendChild(malabel);
document.getElementById('id_de_la_div'+i).appendChild(malabel1);
maDivvide = document.createElement("div");
maDivvide.innerHTML='<br>';
document.getElementById("divbar").appendChild(maDivvide);
i++;
getInfoProduits1();
}
}
}
request.send();
}
else
{
return;
}
}
函数getInfoProduits()
{
var request=new XMLHttpRequest();
if(i“将处理程序附加到元素的事件。每个元素最多执行一次处理程序。”这似乎是您需要的
还有一个纯JS解决方案
fnname = (function(){
var counter = 0;
return function(param1, param2, ...) {
if (counter > 0) {
return;
}
counter++;
// your normal function code here };
})();
只需在html代码中创建div,然后在函数中编辑它的内容。我不明白。乘法多少次?它们乘法多少次?如果你多次调用createElement,它将被创建多次。你能进一步解释吗?是的@Rob我有一个div“divbar”在我的html代码中,在这个函数中,我通过添加新标签和包含产品的div来编辑它information@Th0rbdike当我调用这个函数时,createElement被执行,因此在每次调用中都会创建一个新的div,但我希望div将在第一次调用functiontrx时创建,通过与cons进行调试来检查是否发生了任何事情ole.Add console.log();然后查看发生了什么并找到错误!谢谢,但是我如何将其用于我的函数?如果您不知道这一点,您应该阅读更多关于JavaScript的内容。我建议您执行此函数,传递所需的参数,然后在需要的位置执行该函数。您可以访问它,而不是通过名称(params)。函数没有很好的缩进,这就是为什么我不理解它。