Uncaught TypeError:无法读取未定义的属性'add':将类添加到类名的event.target变量

问题描述 投票:0回答:1

我有此代码:

    var button = $(event.target);
    var videoElement = button.parents(".video-row").find("iframe");
    src = videoElement.attr("src");
    title = videoElement.attr("title");
    description = videoElement.attr("description");
    ...
    button.classList.add("green-button");  //error here

哪个出现此错误:

Uncaught TypeError: Cannot read property 'add' of undefined

button变量似乎有问题。我该如何解决?

javascript
1个回答
0
投票

您正在使用jQuery(button)定义$,但随后将调用.classList就像是button是本机DOM元素一样。并非如此,它是jQuery的一个实例。您需要使用.addClass() jQuery方法:

var button = $(event.target);
var videoElement = button.parents(".video-row").find("iframe");
src = videoElement.attr("src");
title = videoElement.attr("title");
description = videoElement.attr("description");
...
button.addClass("green-button");  //error here
© www.soinside.com 2019 - 2024. All rights reserved.