How to add text from span tag to data-attr by jQuery?

前端 未结 2 1169
你的背包
你的背包 2021-01-16 17:57

How to add text from span tag to data-attr?

Example:
I have this code

2条回答
  •  -上瘾入骨i
    2021-01-16 18:24

    Your

    s should have attribute title="icon"

    You are using a callback to attr() function, so return the value which you need to set inside the function using .text().

    $(document).ready(function () {
        $("[title='icon']").attr('data-attr', function () {
            return $(this).find('span').text();
        }).removeAttr('title');
    });
    

    Demo

提交回复
热议问题