我需要将一个整数传递给'td:nth'

时间:2016-06-24 05:25:45

标签: javascript jquery html

我是java脚本和HTML开发的新手。我需要将变量传递给$('td:nth(int))。我尝试了这段代码,但它不起作用。

var index=2;
alert($('td:nth(index)').html);

1 个答案:

答案 0 :(得分:1)

您可以使用简单的字符串连接来完成此操作。但是你的代码中存在一些错误:

  1. 没有像:nth()这样的伪类选择器,您可以使用 :nth-child() :eq() 伪类选择器或
  2. jQuery对象没有html属性,而是undefined,而是使用 html() 方法。
  3.  var index=2; 
     alert($('td:nth-child(' + index + ')').html());