使用jquery隐藏td标记的值

时间:2016-04-05 09:33:13

标签: jquery html shuffle assign

这是我的小提琴https://jsfiddle.net/tuc1faug/1/ 在这里,我使用jquery为具有特定值的颜色指定了颜色。 颜色将每次都被洗牌。现在我希望这些值隐藏在单元格中,我希望所有这些值都以这个洗牌顺序存储到一个数组中 HTML:

<table  border="5px" width="500px" height="50px" align="center">
    <tr id="colors">
        <td height="50px" orderId="1" bgcolor="red"></td>
        <td height="50px" orderId="6" bgcolor="brown"></td>

        <td height="50px" orderId="5" bgcolor="pink" ></td>
        <td height="50px" orderId="0" bgcolor="blue" ></td>

        <td height="50px" orderId="7" bgcolor="black"></td>
        <td height="50px" orderId="2" bgcolor="green"></td>

        <td height="50px" orderId="4" bgcolor="orange" ></td>
        <td height="50px" orderId="3" bgcolor="yellow"></td>
    </tr>  
</table>

jQuery的:

var arr=[];
var colorCells =document.getElementById('colors').getElementsByTagName('td');
var colors = ["blue","red","green","yellow","orange","pink","brown","black"];
for(var i = 0; i < colorCells.length; i++)  {
    $(colorCells[i]).attr("bgColor", colors.splice(Math.random() * (colors.length),1)) ;
    arr.push(colorCells[i].style.backgroundColor);
}

var colorValues = {"red": 2, "blue":3, "green": 4, "yellow":"1", "orange":5, "black":1, "brown":6, "pink":5};
$("table td").each(function() {
    $(this).html(colorValues[$(this).attr("bgColor")]);
});

4 个答案:

答案 0 :(得分:0)

以下是您需要的代码:

<script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
<script>

	function shuffle(a) {
		var j, x, i;
		for (i = a.length; i; i -= 1) {
			j = Math.floor(Math.random() * i);
			x = a[i - 1];
			a[i - 1] = a[j];
			a[j] = x;
		}
	}

	$(document).ready(function(){
		var colors = ["blue", "red", "green", "yellow", "orange", "pink", "brown", "black"];
		shuffle(colors);
		
		for(var i=0;i<colors.length;i++) {
			var td = $("#colors td").get(i);
			$(td).html(colors[i]);
		}
	});

</script>

<table border="5px" width="500px" height="50px" align="center">
	<tr id="colors">
		<td height="50px"></td>
		<td height="50px"></td>
		<td height="50px"></td>
		<td height="50px"></td>
		<td height="50px"></td>
		<td height="50px"></td>
		<td height="50px"></td>
		<td height="50px"></td>
	</tr>
</table>

shuffle函数来自此答案:https://stackoverflow.com/a/6274381/5119765

答案 1 :(得分:0)

使用以下代码替换脚本。 td中的值是隐藏的,它们存储在本地数组tableContents中。

$(function() {
var arr=[];
var colorCells =document.getElementById('colors').getElementsByTagName('td');
var colors = ["blue","red","green","yellow","orange","pink","brown","black"];
    for(var i = 0; i < colorCells.length; i++)  {
        $(colorCells[i]).attr("bgColor", colors.splice(Math.random() * (colors.length),1)) ;
        arr.push(colorCells[i].style.backgroundColor);
    }

    var tableContents=[];
    var colorValues  = {"red": 2, "blue":3, "green": 4, "yellow":"1", "orange":5, "black":1, "brown":6, "pink":5};
   $("table td").each(function() {
      $(this).html(colorValues[$(this).attr("bgColor")]);
      tableContents.push($(this).text());
      $(this).text('');
   })
 });

答案 2 :(得分:0)

我已经编辑了你的功能。如果我理解你的问题,你需要随机数字的颜色应存储在数组中,你应该隐藏数字显示

如果是,则以下是代码

$(function() {
    var arr=[];
    // New array for adding the color number
    var colorNumber = [];
    var colorCells =document.getElementById('colors').getElementsByTagName('td');
    var colors = ["blue","red","green","yellow","orange","pink","brown","black"];
        for(var i = 0; i < colorCells.length; i++)  {
                var randomColor = Math.random() * (colors.length);
            $(colorCells[i]).attr("bgColor", colors.splice(Math.random() * (colors.length),1)) ;
            arr.push(colorCells[i].style.backgroundColor);
        }

        var colorValues = {"red": 2, "blue":3, "green": 4, "yellow": 7, "orange":5, "black":1, "brown":6, "pink":8};
        $("table td").each(function() {
            // Get the color value from the array and add it in colour numbers array.
            colorNumber.push(colorValues[$(this).attr("bgColor")]);

            // Hide it from display.
            //$(this).html(colorValues[$(this).attr("bgColor")]);   
        });

        // Your colorNumber array.
        alert(colorNumber);
 });

查看已修改的Fiddle

答案 3 :(得分:-1)

添加 - $(this).text('');在内部循环结束,如:

   var colorValues  = {"red": 2, "blue":3, "green": 4, "yellow":"1", "orange":5, "black":1, "brown":6, "pink":5};
   $("table td").each(function() {
      $(this).html(colorValues[$(this).attr("bgColor")]);
      $(this).text('');
   });

它将删除td文本;