为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PIPIONE 2018-11-06 09:13:42
页面中原来的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代码:
查看完整描述

1 回答

?
杨魅力

TA贡献1811条经验 获得超6个赞

$(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)里面应是函数

查看完整回答
反对 回复 2018-12-07
  • 1 回答
  • 0 关注
  • 2174 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号