有两个选择中的每一个()不能与此达到:第一,孩子这一点:第二个孩子
问题描述:
我有以下代码:有两个选择中的每一个()不能与此达到:第一,孩子这一点:第二个孩子
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++;
});
控制台给我这个错误:类型错误:此.find不是一个函数。我想通过$ this作为第一个和第二个元素来达到实际的.etrend_ossze和实际的.tabla_sel。
答
更换
$((this).find(":first-child"))
$((this).find(":second-child"))
通过
$(this).find(":first-child")
$(this).find(":nth-child(1)") //:second-child doesn't exist
答
让我们看看这个行:
$((this).find(":first-child"))
现在,让我们把它分解一下:
$(
(this).find(":first-child")
)
所以浏览器第一次做(this)
(它返回DOM元素对象),然后尝试呼叫find
。对于find
的调用结果,它只会调用jQuery函数$
。但是,它永远不会调用$
,因为在本机DOM元素上没有find
方法,所以出现错误,如您所示。
解决的办法是摆脱多余的括号:
$(this).find(":first-child")
+0
谢谢,我把它们中的许多人 – user2186932 2013-03-19 18:42:04
答
这不是一个正确的方法:
$((this).find(":first-child")).attr("id".....
它应该是:
无论是这样的:
$(this).find(":first-child").attr("id"....
或这样:
$(":first-child",this).attr("id".....
嗯...但是,我从来没有听说过或:second-child
阅读我才意识到这有.etrend_ossze的第一和第二个孩子的影响,我想是的第一和第二个元素这是.etrend_ossze和.tabla_sel对不起,我的误解 – user2186932 2013-03-19 18:25:04