页面中原来的HTML代码:
<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>
我希望通过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>
我现在的jQuery代码:
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");
}
}
)
)
求指点迷津!!
淡淡烟草味2017-05-19 10:42:16
$(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)里面应是函数