element.firstChild.each() undefined



我无法在页面中捕获firstChild.each((元素。

jQuery(function() {
$(".custom-link").firstChild.each(function(t,x) {
if (/*$(x).attr('href').indexOf('isbn=' > -1) &&*/ $(x).attr('href').indexOf('isbn=&' == -1)) { //Check if the ISBN exists
var href = $(x).attr('href');
var isbn = href.substring((href.lastIndexOf('su=')+3),(href.lastIndexOf('su=')+16));});

我已经在不同的应用程序中使用了几乎完全相同的代码行,具有不同的类,并且运行良好。唯一的区别是.firstChild函数,它应该非常简单。

我认为问题可能是类元素在此代码之后加载,所以我尝试将其包装在 .ready(( 函数中,但问题保持不变。

根据页面源代码,页面中有 66 个此类实例。

我还尝试使用:

$(".custom-link:first-child").each(function(t,x) {

但是 css :first-child 命令似乎不适用于类选择器。

我尝试创建一个数组并在其上使用 .each((:

var customlinks = $(".custom-link").firstChild;
customlinks.each(function(t,x) {

但是我收到"未捕获的类型错误:无法读取未定义的属性'每个'",因为自定义链接仍未定义。

你混合了jQuery和DOM函数。jQuery 等价物是:

$(".custom-link > *:first-child").each(...);

您必须将:first-child伪类附加到要选择的元素。.custom-link:first-child选择具有class="custom-link"元素,这些元素本身是其父级的第一个子元素。> *匹配.custom-link的子项,然后:first-child将其限制为每个子项的第一个子项。

您实际上可以将其缩短为

$(".custom-link > :first-child")

因为*是那里的默认值。

最新更新