简体   繁体   中英

Need help understanding jquery syntax

For this segment of code:

    for (var i = 0; i < numbers.length; i++) {
         var imageCrystal = $('<img>');
         imageCrystal.attr('data-num', numbers[i]);
         ....
    }

is, imageCrystal.attr('data-num', numbers[i]); making a data attribute for the image tag that was just created and giving it a class name called "data-num" and then assigning whatever the value is at number[i] ?

Simply put

imageCrystal.attr('data-num', numbers[i]); is a setter

imageCrystal.attr('data-num'); is a getter

If var numbers = [100, 200, 300] and if the img tags are appended to the DOM , it would look something in these lines.

<img data-num="100" />
<img data-num="200" />
<img data-num="300" />

More info: http://api.jquery.com/attr/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM