Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.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从页面上的所有链接获取锚参数并替换目标链接的href_Javascript_Jquery - Fatal编程技术网

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

Javascript 使用JQuery从页面上的所有链接获取锚参数并替换目标链接的href,javascript,jquery,Javascript,Jquery,我正试图从页面上的几个不同锚链接中获取一组href参数,然后将这些参数传递给目标链接。我相信我已经找到了大约95%的解决方案,但我无法传递所有链接参数,它只应用页面上最后一个锚的参数 当我运行document.write(paramstr)时在第一个函数中,我得到了我要寻找的所有参数,但是当我在第二个函数中运行它时,我只得到了最后一个参数。也许我需要在第一个函数中加入一个循环来附加所有参数,这样它们就不会被覆盖 下面是我正在使用的HTML&JS代码: <!--This is the

我正试图从页面上的几个不同锚链接中获取一组href参数,然后将这些参数传递给目标链接。我相信我已经找到了大约95%的解决方案,但我无法传递所有链接参数,它只应用页面上最后一个锚的参数

当我运行
document.write(paramstr)时
在第一个函数中,我得到了我要寻找的所有参数,但是当我在第二个函数中运行它时,我只得到了最后一个参数。也许我需要在第一个函数中加入一个循环来附加所有参数,这样它们就不会被覆盖

下面是我正在使用的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>

如果您希望结果为
?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和相同的参数名称,但无论如何,您可以使用下一个代码来获得此结果结果

  • 无需对循环使用
  • 也无需在
    .attr
    中使用
    功能
    替换
jQuery(文档).ready(函数($){
var paramstr=“”;
$('a.auction\u add\u to\u cart\u button')。每个(函数(){//解析每个链接
var href=$(this.attr('href');//获取锚的href
paramstr+=href.split('/')[3]+'&';//获取感兴趣的参数
});
paramstr=paramstr.slice(0,-1);//删除最后一个&
console.log(paramstr);
//$(“#支付所有拍卖”).attr(“href”,paramstr);
$(“支付所有拍卖”).attr(“数据拍卖”,paramstr);
});

如果您希望结果是
?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和相同的参数名称,但无论如何,您可以使用下一个代码来获得此结果结果

  • 无需对
循环使用

  • 也无需在
    .attr
    中使用
    功能
    替换
  • jQuery(文档).ready(函数($){
    var paramstr=“”;
    $('a.auction\u add\u to\u cart\u button')。每个(函数(){//解析每个链接
    var href=$(this.attr('href');//获取锚的href
    paramstr+=href.split('/')[3]+'&';//获取感兴趣的参数
    });
    paramstr=paramstr.slice(0,-1);//删除最后一个&
    console.log(paramstr);
    //$(“#支付所有拍卖”).attr(“href”,paramstr);
    $(“支付所有拍卖”).attr(“数据拍卖”,paramstr);
    });
    
    
    
    1-您需要提供一些html代码。。以及
    href
    看起来像什么,以及预期的
    href
    应该是什么样子。。2-我认为
    .attr('href'…
    部分应该在
    内。每个
    循环..3-我认为没有必要使用
    来进行
    循环..4-还有
    'a.auction\u add\u to_cart\u button'
    和目标链接之间的关系是什么?请用..你想用
    这里所期望的内容替换
    吗?
    对不起,好的,让我来回答e澄清我的问题..你想要
    href=“#”
    yith-wcact-pay-won-auction=11361,yith-wcact-pay-won-auction=11364,
    11361,11364,…
    或什么?1-你需要提供一些html代码..而
    href
    看起来是什么样子,预期的
    href
    应该是什么样子..2-我认为
    .attr('href'…
    部分应该在
    内。每个
    循环..3-我认为没有必要使用
    来进行
    循环..4-还有
    'a.auction\u add\u to_cart\u button'
    和目标链接之间的关系是什么?请用..你想用
    这里所期望的内容替换
    吗?
    对不起,好的,让我来回答e澄清我的问题..你想让
    href=“#”
    yith-wcact-pay-won-auction=11361,yith-wcact-pay-won-auction=11364,
    11361,11364,…
    或什么??
    <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>
    
    ?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