【问题标题】:jquery hit counterjQuery命中计数器
【发布时间】:2013-04-11 23:22:48
【问题描述】:

我在 ip 上创建了一个独特的点击/点击计数器。

为了显示clic的数量,我在body标签上使用了onload函数。 例如:

<body class="homepage" onload="affiche_compteur('compteur1'); affiche_compteur('compteur2');affiche_compteur('compteur3');affiche_compteur('compteur4');affiche_compteur('compteur5');affiche_compteur('compteur6');">

affiche_compteur(法语)= display_counter

我需要使用 onload 调用我的 html 页面中存在的每个点击计数器。 (它适用于硬编码元素,但在我的情况下,我已经在文档上附加了我的 html 页面中不存在的元素(未硬编码))

有没有办法获取点击计数器的每个 id 以避免在加载时添加每个计数器?

这是我的 ajax 脚本:

function getXhr(){
    var xhr = null; 
    if(window.XMLHttpRequest) {         
        xhr = new XMLHttpRequest(); 
    } else if(window.ActiveXObject) {   
        try {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    } else {
           alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
           xhr = false; 
    } 
    return xhr;
};
function affiche_compteur(id_clics)
{
    var xhr = getXhr();
    xhr.onreadystatechange = function()
    {
        if(xhr.readyState == 4 && xhr.status == 200){
            document.getElementById(id_clics).innerHTML = xhr.responseText;
        }
    };
    xhr.open('POST', './compteur/affiche_compteur.php', true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send('compteurId='+id_clics);
}

这里是被点击元素的html:

<div class="element blog " id="_13">
<a class="link" href="/portfolio/dock.html" onclick="gestionClic('compteur13');">
</a>
</div>

<div class="element blog " id="_12">
<a class="link" href="/portfolio/dock.html" onclick="gestionClic('compteur12');">
</a>
</div>

<div class="element blog " id="_11">
<a class="link" href="/portfolio/dock.html" onclick="gestionClic('compteur11');">
</a>
</div>

对不起我的英语,我是法国人。

【问题讨论】:

  • 只是出于好奇:如果您使用 jQuery,为什么要推出自己的 XHR,而不使用 jQuery ajax() 实现?
  • 我不知道...我这样写是为了能够读取 php 脚本。如果 ip 不存在,则在数据库中写入递增的点击数...

标签: jquery ajax callback


【解决方案1】:
if(xhr.readyState == 4 && xhr.status == 200){
           $(document).on('click','#'+id_clics,function(){
              gestionClic(id_clics);
           });
         document.getElementById(id_clics).innerHTML = xhr.responseText;
 }

【讨论】:

  • 感谢您的回答。此脚本仅用于显示点击计数器。在您的代码中,您只获得点击计数 id 编号 13。我想搜索每个 id 编号点击计数器而不点击元素。实际上,我在 body 中放入了一个 onload 函数,并编写了每个 id 计数器来显示它们。
  • 好的,但是有点击问题。我想在没有任何点击的情况下获得点击次数。我的点击数存储在一个文件中。也许有发现?我已经有一个增加点击然后在点击后显示它的其他脚本。
猜你喜欢
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 2013-12-25
相关资源
最近更新 更多