简体   繁体   English

使用JQuery从页面上的所有链接中获取锚点参数并替换目标链接的href

[英]Using JQuery to get anchor parameters from all links on page and replacing href of target link

I'm trying to obtain a set of href parameters from several different anchor links on a page and then pass those parameters to a target link.我试图从页面上的几个不同锚链接获取一组 href 参数,然后将这些参数传递给目标链接。 I believe I've arrived at about a 95% solution but I'm unable to pass all link parameters, it's only applying the parameter of the last anchor on the page.我相信我已经找到了大约 95% 的解决方案,但我无法传递所有链接参数,它仅应用页面上最后一个锚点的参数。

When I run document.write(paramstr);当我运行document.write(paramstr); inside the first function, I get all the parameters I'm looking for but when I run it inside the second function I'm only getting the last parameter.在第一个函数中,我得到了我正在寻找的所有参数,但是当我在第二个函数中运行它时,我只得到了最后一个参数。 Perhaps I need to incorporate a loop inside the first function to append all the parameters so they don't get overwritten?也许我需要在第一个函数中加入一个循环来附加所有参数,这样它们就不会被覆盖?

Here is the HTML & JS code I'm using:这是我正在使用的 HTML 和 JS 代码:

    <!--This is the target link-->
    <a href="#" id="pay-all-auctions"><button class="button">
    Pay For All Auction Items
    </button>
    </a>
    <!--This is an example of an anchor link; about 20+ of these throughout the page-->
    <a  href="/my-account/my-auction/?yith-wcact-pay-won-auction=11364" 
        class="auction_add_to_cart_button button alt" 
        id="yith-wcact-auction-won-auction"> Add to cart </a>
<script>
    jQuery(document).ready(function($) {
        var href;
        var paramstr;
        $('a.auction_add_to_cart_button').each(function(){    // parse each link
            href = $(this).attr('href');   // get the href of the anchor
            for (var i = 0; i < href.length; i++) {
            paramstr = href.split('/')[3]; // get the parameter of interest
            }
        }); 

        $("a[href*=#]").attr("href", function(){
            // replace the target link href with the parameters of interest
            return href.replace('#',paramstr); 
        });
    });

</script>

Here is the expected output in console:这是控制台中的预期输出:

?yith-wcact-pay-won-auction=11364
(index):640 ?yith-wcact-pay-won-auction=11361
(index):640 ?yith-wcact-pay-won-auction=11357
(index):640 ?yith-wcact-pay-won-auction=11356
(index):640 ?yith-wcact-pay-won-auction=11365
(index):640 ?yith-wcact-pay-won-auction=11376
(index):640 ?yith-wcact-pay-won-auction=11358
(index):640 ?yith-wcact-pay-won-auction=11354
(index):640 ?yith-wcact-pay-won-auction=11345
(index):640 ?yith-wcact-pay-won-auction=11408
(index):640 ?yith-wcact-pay-won-auction=11407
(index):640 ?yith-wcact-pay-won-auction=11406
(index):640 ?yith-wcact-pay-won-auction=11405
(index):640 ?yith-wcact-pay-won-auction=11404
(index):640 ?yith-wcact-pay-won-auction=11398
(index):640 ?yith-wcact-pay-won-auction=11397
(index):640 ?yith-wcact-pay-won-auction=11396
(index):640 ?yith-wcact-pay-won-auction=11395
(index):640 ?yith-wcact-pay-won-auction=11394
(index):640 ?yith-wcact-pay-won-auction=11393
(index):640 ?yith-wcact-pay-won-auction=11392
(index):640 ?yith-wcact-pay-won-auction=11391
(index):640 ?yith-wcact-pay-won-auction=11390
(index):640 ?yith-wcact-pay-won-auction=11389
(index):640 ?yith-wcact-pay-won-auction=11388

If you want the result to be ?yith-wcact-pay-won-auction=11363&?yith-wcact-pay-won-auction=11364&?yith-wcact-pay-won-auction=11365&?yith-wcact-pay-won-auction=11366 I don't know how you'll work with something like this specially its not a valid url and the same parameter name but anyway you can use the next code to get this result如果您希望结果为?yith-wcact-pay-won-auction=11363&?yith-wcact-pay-won-auction=11364&?yith-wcact-pay-won-auction=11365&?yith-wcact-pay-won-auction=11366我不知道你将如何处理这样的事情,特别是它不是一个有效的 url 和相同的参数名称,但无论如何你可以使用下一个代码来获得这个结果

  • No need to use for loop无需使用for循环
  • Also no need to use function and replace in .attr也不需要在.attr使用functionreplace

 jQuery(document).ready(function($) { var paramstr =""; $('a.auction_add_to_cart_button').each(function(){ // parse each link var href = $(this).attr('href'); // get the href of the anchor paramstr += href.split('/')[3]+'&'; // get the parameter of interest }); paramstr = paramstr.slice(0, -1); // remove the last & console.log(paramstr); //$("#pay-all-auctions").attr("href", paramstr); $("#pay-all-auctions").attr("data-auctions", paramstr); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!--This is the target link --> <a href="#" id="pay-all-auctions"><button class="button"> Pay For All Auction Items </button> </a> <!--This is an example of an anchor link; about 20+ of these throughout the page --> <a href="/my-account/my-auction/?yith-wcact-pay-won-auction=11363" class="auction_add_to_cart_button button alt" id="yith-wcact-auction-won-auction"> Add to cart </a> <a href="/my-account/my-auction/?yith-wcact-pay-won-auction=11364" class="auction_add_to_cart_button button alt" id="yith-wcact-auction-won-auction"> Add to cart </a> <a href="/my-account/my-auction/?yith-wcact-pay-won-auction=11365" class="auction_add_to_cart_button button alt" id="yith-wcact-auction-won-auction"> Add to cart </a> <a href="/my-account/my-auction/?yith-wcact-pay-won-auction=11366" class="auction_add_to_cart_button button alt" id="yith-wcact-auction-won-auction"> Add to cart </a>

Finally: The result It's not a valid value to add to the anchor href with this type of values you can use data attribute instead最后:结果 使用这种类型的值添加到锚点 href 的值不是有效值,您可以改用data属性

Additional Information附加信息

  • The parameter in href shoud look like ?p1=1&p2=2&p3=3 href的参数应该类似于?p1=1&p2=2&p3=3

  • If you have a same parameter in the url ?p1=1&p1=2 for example php $_GET['p1'] will return the last parameter value如果您在 url 中有相同的参数?p1=1&p1=2例如 php $_GET['p1']将返回最后一个参数值

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

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