首页 > 解决方案 > 我不断收到“TypeError:未定义不是对象”错误消息

问题描述

我一直在尝试制作一个脚本,让用户在他们的 iOS 或 iPadOS 设备上安装配置文件,我的代码是:

function downloadProfile(profileName, configurationProfile) {
    var element = document.createElement("a");
    element.setAttribute("href", "data:application/x-apple-aspen-config," + encodeURIComponent(configurationProfile));
    element.setAttribute("download", profileName + ".mobileconfig");

    element.body.appendChild(element);

    element.click();

    element.body.removeChild();
}

问题是,我不断收到一条错误消息说Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'element.body.appendChild')。有谁知道修复?当然,这不是整个代码,它只是我需要帮助的部分。

标签: javascriptpromisetypeerror

解决方案


您创建的元素没有属性主体。我假设您想将新元素添加到文档中,如下所示:

document.body.appendChild(element);

检查你的例子:https ://www.w3schools.com/jsref/met_document_createelement.asp


推荐阅读