javascript - JQuery each traverses the A tag to obtain the href and takes the value of the attribute in the link and adds the attribute of A
伊谢尔伦
伊谢尔伦 2017-05-19 10:40:16
0
1
643

Original HTML code in the page:

<a class="item" href="http://sample.com/do.php?id=1234">test1</a>
<a class="item" href="http://sample.com/do.php?id=12345">test2</a>
<a class="item" href="http://sample.com/do.php?id=432141">test3</a>

The final output effect I hope to achieve through jquery:

<a class="item" biz_itemid="1234" isconvert=1 href="http://sample.com/do.php?id=1234">test1</a>
<a class="item" biz_itemid="12345" isconvert=1 href="http://sample.com/do.php?id=12345">test2</a>
<a class="item" biz_itemid="432141" isconvert=1 href="http://sample.com/do.php?id=432141">test3</a>

My current jQuery code:


function GetUrlParms(){
var args=new Object(); 
    var query=location.search.substring(1);//获取查询串 
    var pairs=query.split("&");//在逗号处断开 
    for(var i=0;i<pairs.length;i++){ 
        var pos=pairs[i].indexOf('=');//查找name=value 
        if(pos==-1) continue;//如果没有找到就跳过 
        var argname=pairs[i].substring(0,pos);//提取name 
        var value=pairs[i].substring(pos+1);//提取value 
        args[argname]=unescape(value);//存为属性 
    }
return args;
}
 $(document).ready(
    $(".item").each(
        function(){
            var href = $(this).attr("href");
            var args = new Object();
            itemid = GetUrlParms("href");
            if(args["id"]!=undefined){
                var id = args["id"]
                $(this).attr('isconvert','1');
                $(this).attr('biz-itemid',"id");
            }
        }
    )
)

Please give me some advice! !

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
淡淡烟草味
$(document).ready(function() {
    $(".item").each(
        function() {
            var href = $(this).attr("href"),
                itemid = href.match(/\?id=(\d+)/) ? href.match(/\?id=(\d+)/)[1] : 0;
            itemid && $(this).attr({'isconvert':'1','biz-itemid':itemid});
        }
    )
})

$(document).ready(function) should be a function

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!