doufan6886 2010-09-22 11:37
浏览 135
已采纳

使用引用URL获取DOM数据

Suppose we have a page with the following structure:

<li id="A">
<span class="some class">some content
<a href="http://www.example.com">http://www.example.com</a>
</span>
</li>

<li id="B">
<span class="some class">some content
<a href="http://www.example.com">http://www.example.com</a>
</span>
</li>

<li id="C">
<span class="some class">some content
<a href="http://www.example.com">http://www.example.com</a>
</span>
</li>

Is it possible, with PHP or JS, to grab the list id (A,B,C) and append it along with the referring URL when a person clicks on any one of the three links?

  • Need to know specifically which one of the three list tags the click originated from
  • I don't have write access to the originating page
  • 写回答

2条回答 默认 最新

  • dsgwii4867 2010-09-22 12:11
    关注

    UPDATE

    In light of your revelation that you have no access to the original page, you've got no chance of finding out anything about the specific link that was clicked to arrive at your page. You can get the referrer with document.referrer.

    Previous answer for posterity

    The following function will capture a click on any link within the container element with the specified id and add a query string containing the id of the container element and the URL of the current page to the URL:

    function modifyLink(containerId) {
        var el = document.getElementById(containerId);
        el.onclick = function(evt) {
            evt = evt || window.event;
            var target = evt.target || evt.srcElement;
            if (target.tagName == "A") {
                window.location.href = target.href + "?id=" + encodeURIComponent(containerId) +
                    "&referrer=" + encodeURIComponent(window.location.href);
    
                return false; // Cancel the default link behaviour
            }
        };
    }
    
    modifyLink("A");
    modifyLink("B");
    modifyLink("C");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用