JQuery each遍历A标签获取href 并取链接中属性的值及增加A的属性

页面中原来的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=" 
<a class="item" biz_itemid="12345" isconvert=1 href=" 
<a class="item" biz_itemid="432141" isconvert=1 href="http://sample.com/do.php?id=432141">test3</a>

我现在的jQuery代码:

https://img.mukewang.com/5c0a3434000159dd05230533.jpg

PIPIONE
浏览 2175回答 1
1回答

杨魅力

$(document).ready(function() {&nbsp; &nbsp; $(".item").each(&nbsp; &nbsp; &nbsp; &nbsp; function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var href = $(this).attr("href"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemid = href.match(/\?id=(\d+)/) ? href.match(/\?id=(\d+)/)[1] : 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemid && $(this).attr({'isconvert':'1','biz-itemid':itemid});&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; )})$(document).ready(function)里面应是函数
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript