0

我有以下代码:

var tabla_szam = 1;
var etk_szam = 1;
$(".etrend_ossze, .tabla_sel").each(function () {
   if (etk_szam == 5) {
      tabla_szam++;
      etk_szam = 1;
   }
$((this).find(":first-child")).attr("id", "tabla_" + tabla_szam + "_" + etk_szam); //i want to reach the actual .etrend_ossze and .tabla_sel
$((this).find(":second-child")).attr("id", "tabla_sel" + tabla_szam + "_" + etk_szam);
etk_szam++;
});

控制台给了我这个错误:TypeError: this.find is not a function。我想通过 $this 作为第一个和第二个元素来达到实际的 .etrend_ossze 和实际的 .tabla_sel 。

4

3 回答 3

1

代替

$((this).find(":first-child"))
$((this).find(":second-child"))

经过

$(this).find(":first-child")
$(this).find(":nth-child(1)") //:second-child doesn't exist
于 2013-03-19T18:20:06.160 回答
0

这不是正确的方法:

 $((this).find(":first-child")).attr("id".....

它应该是:

无论是这种方式:

$(this).find(":first-child").attr("id"....

或者这样:

$(":first-child",this).attr("id".....

嗯...但是,我从未听说过或读过:second-child

于 2013-03-19T18:26:26.653 回答
0

Let's have a look at this line:

$((this).find(":first-child"))

Now let's break it up a bit:

$(
    (this).find(":first-child")
)

So the browser first does (this) (which returns the DOM element object) and then tries to call find. It would only call the jQuery function $ on the result of the call to find. However, it never gets as far as calling $, because there is no find method on a native DOM element, so there is an error, as you show.

The solution is to get rid of the extra brackets:

$(this).find(":first-child")
于 2013-03-19T18:22:53.587 回答