开发者

Jquery get attribute of few ul to create another attribute

开发者 https://www.devze.com 2022-12-24 17:35 出处:网络
I have the 开发者_开发百科following HTML: <ul actualpage=0> <li/> <li/> .... </ul>

I have the 开发者_开发百科following HTML:

<ul actualpage=0>
<li/>
<li/>
....
</ul>

<ul actualpage=0>
<li/>
<li/>
....
</ul>

Im trying to get the value of actualpage of each ul and create a new attribute. Its easy but not in one jquery sentence... Its possible? Until now i have the following line (between ## simbols the missing part that i need.

    /*
select all uls with attribute actualpage and create a new attribute on each with the current actualpage value
    */

$('ul[actualpage]').attr('newactualpage',##Current value of actualpage attr of UL##);


Well maybe this isn't as nice as you'd like, but

$('ul[actualpage]').each(function(_, ul) { $(ul).attr('newactualpage', $(ul).attr('actualpage')); });


One might think that

$('ul[actualpage]').attr('newactualpage',$(this).attr('actualpage'))

is the answer.

However, this is evaluated before the call to attr, so it's going to equal whatever this equals in the calling context.

You could use:

$('ul[actualpage]').attr('newactualpage',function(){
    return $(this).attr('actualpage');
});

or this:

$('ul[actualpage]').each(function()
{
     $(this).attr('newactualpage',$(this).attr('actualpage'));
};

In both, this refers to the element that your selector matched.


You can use function as second argument for .attr(), eliminating the need of .each():

$('ul[actualpage]').attr('newactualpage', function() { return $(this).attr('actualpage') });
0

精彩评论

暂无评论...
验证码 换一张
取 消