$document.append 不起作用



我有一个函数,应该在页面上的特定位置附加图像。该函数如下所示:

// Attaches the image to the garment
var _attachToGarment = function (container, panelId, path) {
    // If we have no path, exit the function
    if (!path || !panelId)
        return;
    // Get our elements
    var element = container[0].querySelectorAll('[id="' + panelId + '"]'),
        img = angular.element('<img class="img-responsive" src="' + path + '" />')[0];
    // If we have an element
    if (element) {
        // Get the dimensions
        var dimensions = element[0].getBoundingClientRect();
        // Update the images' style attributes
        img.style.width = dimensions.width + 'px';
        img.style.top = dimensions.top + 'px';
        img.style.left = dimensions.left + 'px';
        img.style.position = 'absolute';
        console.log(img);
        // Append the image to our document
        $document.append(img);
    }
};

我遇到的问题是图像没有附加到任何地方的文档。如果我更改行:

$document.append(img);

自:

container.append(img);

图像已附加,但显然在错误的位置。

有谁知道为什么$document.append不起作用?

您仍然需要为其提供一个要附加的元素。 你想把它附加到什么?

你需要做这样的事情:

$document.find('body').append(img);

最新更新