loops - Using JavaScript (jQuery), how can I cycle through all 'a' tags on a page, one at a time, and assign an individual style to each? -


Try to explain to me ... let's say that I have the following 'A' tag on a page:

  & lt; A href = "img1.jpg" class = "myClass" alt = "0,0,600,200" & gt; & Lt; / A & gt; & Lt; A href = "img2.jpg" square = "miracles" alt = "200,0,600,75" & gt; & Lt; / A & gt; & Lt; A href = "img3.jpg" square = "myClass" alt = "275,0,600,200" & gt; & Lt; / A & gt; & Lt; A href = "img4.jpg" square = "myClass" alt = "475,0,600,50" & gt; & Lt; / A & gt; & Lt; A href = "img5.jpg" square = "miracles" alt = "525,0,600,100" & gt; & Lt; / A & gt; & Lt; A href = "img6.jpg" square = "miracles" alt = "625,0,600,200" & gt; & Lt; / A & gt;  

And I want to cycle through the class 'myClass' through all the 'A' tags and read alt tags and specify specific 'style' attributes depending on its value. I do

I tried the following:

Find the value of the alt alt = $ ("a.myclass"). Attr ('alt'); // array array values ​​in array array = tmp.split (','); // Specify an array value to a dedicated variable var a = partsArray [0]; Var B = PartsAre [1]; Var C = Partsir [2]; Var D = Parts Other [3]; // Create inline style string var style; Style = style + "status: full; display: block;"; Style = style + "border: 1px solid red;"; Style = style + "width:" + c + "px;"; Style = style + "height:" + d + "px;"; Style = style + "top:" + a + "px;"; Style = style + "left:" + b + "px;"; // style attribute and its value $ ('a.myClass'). Attri ('style', style);

But this first assumes the 'A' tag as 'alt' attribute and specifies the style on all 'A' tags. What I'm trying to do, read every 'A' tag, get the 'Alt' value and assign to the style that 'A' tag reads only the next 'A' tag ...

Is there a way to accomplish this without providing a unique ID or class for an 'id'?
I'm totally new to jQuery and have tried this issue and spent hours and can not figure out how.
Any help please

You can use the method like this:

  $ ('A.myClass') Each (function () {var tmp = $ (this) .attr ('alt'); // your next code ...});  

will be repeated on all links with each class myClass and the alt value of each link will be stored The tmp variable that you can work on.


Comments