开发者

How to copy contents of a span into its title attribute?

开发者 https://www.devze.com 2023-03-23 17:27 出处:网络
I have this code now var i, $spanc = jQuery(\"#menu\").find(\"span\").filter(\":not(.stepnumber)\"); for(i in $spanc){

I have this code now

var i,
$spanc = jQuery("#menu").find("span").filter(":not(.stepnumber)");
        for(i in $spanc){
            $spanc.eq(i).attr("title", $spanc.eq(i).text());
         }

is there a way I can make it simpler开发者_运维问答? Thanks for any suggestion or help


Yep:

jQuery("#menu span:not(.stepnumber)").each(function() {
    $(this).attr('title', $(this).text());
});

You can use .each() to iterate over a jQuery collection. this refers to the current object in the loop. You can also combine selectors into one string, $('#menu span') is the same thing as $('#menu').find('span').

0

精彩评论

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