Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取背景img url并使用它创建新的<;img>;要素_Javascript_Jquery_Html - Fatal编程技术网

Javascript 获取背景img url并使用它创建新的<;img>;要素

Javascript 获取背景img url并使用它创建新的<;img>;要素,javascript,jquery,html,Javascript,Jquery,Html,我有两个超链接,使用背景图像。并希望弹出一个模式窗口,其中的图像源与背景相同。但似乎无法让它发挥作用 我的js代码目前 $('a.portfolio-item').click(function(event){ event.preventDefault(); var content = $('.modal-body'); content.empty(); var img = $(this).css('background-image').replace(/^url\

我有两个超链接,使用背景图像。并希望弹出一个模式窗口,其中的图像源与背景相同。但似乎无法让它发挥作用

我的js代码目前

$('a.portfolio-item').click(function(event){
    event.preventDefault();
    var content = $('.modal-body');
    content.empty();
    var img = $(this).css('background-image').replace(/^url\(|\)$/g, "");
    content.html('<img src="'+ img +'">');   <---- need help here 
    $(".modal-profile").modal({show:true});
});
$('a.portfolio-item')。单击(函数(事件){
event.preventDefault();
变量内容=$('.modal body');
content.empty();
var img=$(this.css('background-image').replace(/^url\(|\)$/g,”);
html(“”);

我在这里做错了什么吗?

您需要如下更改从src中删除
请查找下面的代码片段以供参考

content.html('<img src='+ img +'>');

    &时代; 模态头 模态中的一些文本

    接近
    如果你为
    a.portfolio-item
    发布一些HTML+CSS示例,这样我们就可以看到你正在使用的源内容了。编辑后包含了htmltank you@Curiousdev,这就解决了我的问题。不需要两者的原因是什么?”dis背后的原因是,当你获取图像值时,它已经在这个函数中用
    $(this.css('background-image')
    包装了
    ”,因此它不再需要…:-)这是我的荣幸:-)我现在面临的另一个问题是与此模式功能相关的。你能看看我的网站吗?通过执行just和var content=$('.modal body.portfolio')使其正常工作;谢谢你的帮助!
    content.html('<img src='+ img +'>');