簡體   English   中英

動態更改頁面上所有鏈接的href,以同時打開主鏈接和輔助鏈接

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

我的網頁上有許多按鈕和鏈接,如下所示:

<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>

我想一個JavaScript腳本將通過該頁面,並動態添加第二個鏈接到href,這樣

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

在頁面上的每個鏈接上。

我的網站上有很多頁面,因此非常希望有人可以通過僅在<head>部分中實現javascript段並通過動態更改頁面上的所有鏈接來向我展示如何執行此操作。

謝謝!

假裝您有一個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');
        }

//加載時調用它。

 changeHref();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM