Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于放置计数链接并指定为变量?jquery_Javascript_Jquery - Fatal编程技术网

Javascript 基于放置计数链接并指定为变量?jquery

Javascript 基于放置计数链接并指定为变量?jquery,javascript,jquery,Javascript,Jquery,所以我有一个id叫做mail\u body,在mail\u body中有一堆随机文本和随机链接。我所说的随机是指从数据库中调用的未知,而不是真正的随机 <div id="mail_body"> Random unknown text with <a href="..."> a random </a> amount of links <a href="..."> ins

所以我有一个id叫做mail\u body,在mail\u body中有一堆随机文本和随机链接。我所说的随机是指从数据库中调用的未知,而不是真正的随机

<div id="mail_body">
    Random unknown text with 

   <a href="...">
        a random
    </a> 

    amount of links 

    <a href="...">
        inside it
    </a>
</div>

如何计算链接编号与div#mail _body的位置相关?

您只需在所有链接的集合中获取计数的
索引

$(document).on("click", "#mail_body a", function(event) {

    event.preventDefault();

    var $allLinks = $("#mail_body").find("a"); // collect
    var id = $allLinks.index( this );          // and find index

    window.open('trackinglink.php?l='+id, "_blank", '', false);
});

明亮的ty:)@Bruce y'w y'w:)
$(document).on("click", "#mail_body a", function(event) {

    event.preventDefault();

    var $allLinks = $("#mail_body").find("a"); // collect
    var id = $allLinks.index( this );          // and find index

    window.open('trackinglink.php?l='+id, "_blank", '', false);
});