Jquery更改变量div的CSS

Jquery更改变量div的CSS,jquery,jquery-selectors,Jquery,Jquery Selectors,我有一个表,表中有一堆td类markTD,它们每个属性值都有一个不同的整数。我想单击名为mark的按钮,只更改td中一个的颜色 $('#mark').click(function(){ var h2 = parseInt($('#h2').attr('value'), 10); //h2 returns the number 3 $('#markTD').find('value',h2).css("color","red"); //this is the li

我有一个表,表中有一堆
td
markTD
,它们每个属性
值都有一个不同的整数。我想单击名为
mark
的按钮,只更改
td
中一个的颜色

 $('#mark').click(function(){
    var h2 = parseInt($('#h2').attr('value'), 10);
    //h2 returns the number 3
        $('#markTD').find('value',h2).css("color","red");  //this is the line that's not working

}
HTML


问题编号:
此行不起作用,因为
find
只接受一个参数,即选择器

尝试使用
$(“#markTD”)。每个
,然后使用
.css()
返回并检查所需的值。

您可以使用.eq()方法选择要更改的开关td,例如使用第一个元素jQuery数组的0值

  $('#markTD').find('value',h2).eq(0).css("color","red"); 
查看javascript中的答案:
1.在函数中,获取集合中的所有tds。
2.获取集合的长度。
3.获取从0到集合(长度-1)的随机值。
4.使用随机数访问一个tds并更改颜色。
请参见下面的示例html:
函数ChangeColor()
{
var tdCol=document.getElementsByClassName(“markTD”);
变量长度=tdCol.length;
var randomtIndex=Math.floor(Math.random()*(长度-0))+0;
/*清除所有颜色//此循环并不重要。我使用它来清除所有颜色,以防出现彩色td*/
对于(变量i=0;i
这是我的实际代码。我只是没有把它写在问题里
 $('#markTD').find('value',h2).css("color","red");
  $('#markTD').find('value',h2).eq(0).css("color","red"); 
See the answer in javascript:

1. In your function get all the tds in collection.

2. Get the length of the collection.

3. Get a random value from 0 to the (length - 1) of your collection.

4. Use the random number to access one of your tds and change the colour.

See sample html below:


<html>
<head>
<script>

function ChangeColor()
{

    var tdCol = document.getElementsByClassName("markTD");

    var length = tdCol.length;

    var randomTdIndex =  Math.floor(Math.random() * (length - 0)) + 0 ;

 /* clear all colors // this loop is not important. I used it to clear all colors just incase there was a coloured td */

   for(var i = 0 ; i < length ; i++)
   {
    tdCol[i].style.backgroundColor = "white";
   }

    tdCol[randomTdIndex].style.backgroundColor = "red";

} 

</script>
</head>

<body>
<table>
<tr><td class="markTD" >td 1</td><td class="markTD">td 2</td></tr>
<tr><td class="markTD">td 2</td><td class="markTD">td 4</td></tr>
<tr><td class="markTD">td 5</td><td class="markTD">td 6</td></tr>
</table>

<input type="button" onclick="ChangeColor()" value="Update TD" />
</body>
</html>