简体   繁体   English

动态更改页面上所有链接的href,以同时打开主链接和辅助链接

[英]dynamically change the href of all links on the page to open the primary link and secondary link at the same time

my web page has many buttons and links like so: 我的网页上有许多按钮和链接,如下所示:

<div class="box"> 
<a href="http://foo.com/index.php/products/view/bar">
<div class="bar" style="margin-top:337px;cursor:pointer;"></div></a> 
</div>

and i would like a javascript script that would go through the page and dynamically add a second link to the href so it would be 我想一个JavaScript脚本将通过该页面,并动态添加第二个链接到href,这样

<a href="#" onclick="window.open('http://foo.com/index.php/products/view/bar');
    window.open('bar.com');"></a>

on each link in the page. 在页面上的每个链接上。

I have many pages in my site so would very much like if someone could show me how to do this by just implementing a javascript segment in the <head> section and have it dynamically change all the links on the page. 我的网站上有很多页面,因此非常希望有人可以通过仅在<head>部分中实现javascript段并通过动态更改页面上的所有链接来向我展示如何执行此操作。

Thanks! 谢谢!

Just pretend you have an A tag " <a href="http://foo.com/index.php/products/view/bar">a</a> " 假装您有一个A标签“ <a href="http://foo.com/index.php/products/view/bar">a</a>

var createDelegate = function (obj, handler) {
                return function () { return handler.apply(obj, arguments); };
            };

            var changeHref = function () {
                var hrefs = document.getElementsByTagName("A");
                for (var i = 0; i < hrefs.length; i++) {
                    hrefs[i].attachEvent('onclick', createDelegate(hrefs[i], function () { openLink(this); return false; }));
                }
            }

        var openLink = function (obj) {
            var str = new String(obj.href);
            str = str.substr(str.lastIndexOf('/') + 1);
            window.open(obj.href);
            window.open(str + '.com');
        }

// call it when loaded. //加载时调用它。

 changeHref();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM