Sunday, 15 May 2011

javascript - Append image to td element with no id or value -


I have an array of image elements, I use a function to make the array random, and I return them I would like to add HTML table in its random order though I am trying to avoid giving my ID to each TD element because it is very low ... I am thinking that it is possible to add the image to the TD element because the ID is not.

There are about 12 rows in the HTML table which will be like this:

  & lt; Table class = "piceter" & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; / TD & gt; & Lt; / TR & gt; Etc ...  

JS

  function randomizePieces (myArray) {for (var i = myArray.length - 1; i>;; i - -) {var j = Math.floor (Math.rendum () * (i + 1)); Var temp = myArray [i]; Mary [i] = Mary [ja]; Mary [j] = temporary; } Return array; }  

Containing table is already built and you can call each TD and update it in the background with just plain JS.

  // starts with 'table' element var tbl = document.getElementsByClassName ("piecetray"); // Receive all the hair lines `TR` of" tables "var trs = tbl [0]. Child node [1] .getElementsByTagName ("tr"); Var trlen = trs.length; // Just a test image var host = "http://upload.wikimedia.org"; Var img = host + "/wikipedia/commons/thumb/2/25/Red.svg/200px-Red.svg.png"; Get the `td` for // rows at the` t`` for the rows (var i = 0; i & lt; trlen; i ++) {// for this line var tds = trs [i]. GetElementsByTagName ("td"); Var tdlen = tds.length; Repeat on `TD` cells for // (` n = 0; n & lt; tdlen; n ++) (// set 'background image' tds [n]. Style.background path = "url (\" " + + + "+" ")";}}  

Look, it is expected that at least this will tell you in the right direction.


No comments:

Post a Comment