jquery从图像URL克隆图像

jquery从图像URL克隆图像,jquery,Jquery,我想更改此codepen小提琴,这样它就不用使用DOM中找到的图像进行克隆,而是使用来自指定URL的图像进行克隆,例如:(来自背景URL的图像): 如何使用jQuery从图像URL创建对象 我试过用这种方法,但上面的小提琴不起作用: var imgtodrag = document.createElement('img'); imgtodrag.src = "http://graphics.cars.com/images/core/buying-guide-suv.png"; 实际测

我想更改此codepen小提琴,这样它就不用使用DOM中找到的图像进行克隆,而是使用来自指定URL的图像进行克隆,例如:(来自背景URL的图像):

如何使用jQuery从图像URL创建对象

我试过用这种方法,但上面的小提琴不起作用:

var imgtodrag = document.createElement('img');
    imgtodrag.src = "http://graphics.cars.com/images/core/buying-guide-suv.png";
实际测试代码:

    var cart = $('#navigation .cart');

    var imgtodrag_actual = $(this).parents('.search-result').find(".main-thumb .the-thumb"); //This is where the animation starts from

    var imgtodrag_img = imgtodrag_actual.css("background-image"); // it is NOT an image element but a div with a background image - this is where the complication comes from       
    imgtodrag_img = imgtodrag_img.replace('url("','').replace('")',''); //the URL of that background image

    var imgtodrag = document.createElement('img'); //now we need to create an object that will be cloned
    imgtodrag.src = imgtodrag_img; //Set the image source of object, taken from the background image above

    if (imgtodrag) {
        var imgclone = imgtodrag.clone()
            .offset({
            top: imgtodrag_actual.offset().top,
            left: imgtodrag_actual.offset().left
        })
            .css({
            'opacity': '0.5',
                'position': 'absolute',
                'height': '150px',
                'width': '150px',
                'z-index': '100'
        }) ......
我真的希望这是有意义的


基本上,我想使用fiddle代码,但是fiddle中的图像来自一个元素。我需要它来自元素的背景图像。

问题是
imgtodrag
是一个dom元素,它没有像
offset
这样的方法。另外,由于您正在创建
img
的新实例,因此无需克隆它

在本例中,您可以使用jQuery创建新的
img
元素

var imgtodrag = $('<img />', {
    src: imgtodrag_img //Set the image source of object, taken from the background image above
}) //now we need to create an object that will be cloned


var imgclone = imgtodrag.offset({
    top: imgtodrag_actual.offset().top,
    left: imgtodrag_actual.offset().left
}).css({
    'opacity': '0.5',
        'position': 'absolute',
        'height': '150px',
        'width': '150px',
        'z-index': '100'
})

var-imgtodrag=$(“问题在于
imgtodrag
是一个dom元素,它没有像
offset
这样的方法。另外,由于您正在创建
img
的新实例,因此不需要克隆它

在本例中,您可以使用jQuery创建新的
img
元素

var imgtodrag = $('<img />', {
    src: imgtodrag_img //Set the image source of object, taken from the background image above
}) //now we need to create an object that will be cloned


var imgclone = imgtodrag.offset({
    top: imgtodrag_actual.offset().top,
    left: imgtodrag_actual.offset().left
}).css({
    'opacity': '0.5',
        'position': 'absolute',
        'height': '150px',
        'width': '150px',
        'z-index': '100'
})

var-imgtodrag=$(“问题在于
imgtodrag
是一个dom元素,它没有像
offset
这样的方法。另外,由于您正在创建
img
的新实例,因此不需要克隆它

在本例中,您可以使用jQuery创建新的
img
元素

var imgtodrag = $('<img />', {
    src: imgtodrag_img //Set the image source of object, taken from the background image above
}) //now we need to create an object that will be cloned


var imgclone = imgtodrag.offset({
    top: imgtodrag_actual.offset().top,
    left: imgtodrag_actual.offset().left
}).css({
    'opacity': '0.5',
        'position': 'absolute',
        'height': '150px',
        'width': '150px',
        'z-index': '100'
})

var-imgtodrag=$(“问题在于
imgtodrag
是一个dom元素,它没有像
offset
这样的方法。另外,由于您正在创建
img
的新实例,因此不需要克隆它

在本例中,您可以使用jQuery创建新的
img
元素

var imgtodrag = $('<img />', {
    src: imgtodrag_img //Set the image source of object, taken from the background image above
}) //now we need to create an object that will be cloned


var imgclone = imgtodrag.offset({
    top: imgtodrag_actual.offset().top,
    left: imgtodrag_actual.offset().left
}).css({
    'opacity': '0.5',
        'position': 'absolute',
        'height': '150px',
        'width': '150px',
        'z-index': '100'
})

var imgtodrag=$('不要克隆现有图像,而是创建自己的图像对象。然后应用正确的源代码并继续使用代码笔逻辑

$('<img>').attr('src', src_for_image)
// continue with the code pen
$('

这里有一个更新的代码笔:

不要克隆现有图像,而是创建自己的图像对象。然后应用正确的源代码,继续使用代码笔逻辑

$('<img>').attr('src', src_for_image)
// continue with the code pen
$('

这里有一个更新的代码笔:

不要克隆现有图像,而是创建自己的图像对象。然后应用正确的源代码,继续使用代码笔逻辑

$('<img>').attr('src', src_for_image)
// continue with the code pen
$('

这里有一个更新的代码笔:

不要克隆现有图像,而是创建自己的图像对象。然后应用正确的源代码,继续使用代码笔逻辑

$('<img>').attr('src', src_for_image)
// continue with the code pen
$('

这是一个更新的代码笔:

谢谢,这是我一问这个问题就开始尝试的。它有效。我给这个打勾是因为它是一个更完整的答案。谢谢,这是我一问这个问题就开始尝试的。它有效。我给这个打勾是因为它是一个更完整的答案。谢谢,这是我开始尝试的我问了这个问题。它很有效。我给这个打勾,因为它是一个更完整的答案。谢谢,这是我一问这个问题就开始尝试的。它有效。我给这个打勾,因为它是一个更完整的答案。非常感谢Jack非常感谢Jack非常感谢Jack非常感谢Jack