Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 使用jquery将所有图像包装为html字符串_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用jquery将所有图像包装为html字符串

Javascript 使用jquery将所有图像包装为html字符串,javascript,jquery,html,Javascript,Jquery,Html,环顾四周,找不到任何有用的东西 我需要一个函数,用匹配href的包装图像替换字符串中的所有图像,然后返回一个字符串 content = "<html>"; var $content = $(content); $('img', $content).each(function () { $(this).html().replace($(this).html(),'<a href="' + $(this).attr('src') + '" target="_blan

环顾四周,找不到任何有用的东西

我需要一个函数,用匹配href的包装图像替换字符串中的所有图像,然后返回一个字符串

content = "<html>";
var $content = $(content);
$('img', $content).each(function () {
        $(this).html().replace($(this).html(),'<a href="' + $(this).attr('src') + '" target="_blank" class="thumbnailLink"><img src="' + $(this).attr('src') + '" style="' + $(this).attr('style') +'"></a>')                    
    }); 
    return  $content.html();
content=”“;
var$content=$(content);
$('img',$content)。每个(函数(){
$(this.html().replace($(this.html(),'')
}); 
返回$content.html();

您可以只设置
html
,而不是读取和替换它

$('img', $content).each(function () {
    var src = $(this).attr('src');
    var style = $(this).attr('style');
    $(this).html('<a href="' + src + '" target="_blank" class="thumbnailLink"><img src="' + src + '" style="' + style +'"></a>'); 
});
$('img',$content)。每个(函数(){
var src=$(this.attr('src');
var style=$(this.attr('style');
$(this.html(“”);
});

这里有一个可能的解决方案,希望它能为您提供帮助

content = '<div class="img"><img src="fjords.jpg" alt="Fjords" width="300" height="200"><div class="desc">Add a description of the image here</div></div><div class="img"><img src="forest.jpg" alt="Forest" width="300" height="200"><div class="desc">Add a description of the image here</div></div><div class="img"><img src="lights.jpg" alt="Northern Lights" width="300" height="200"><div class="desc">Add a description of the image here</div></div><div class="img"><img src="mountains.jpg" alt="Mountains" width="300" height="200"><div class="desc">Add a description of the image here</div></div>';
html = $( content );


$('img',html).each(function () {
$(this).wrap('<a href="' + $(this).attr('src') + '" target="_blank" class="thumbnailLink"> </a>');    

    }); 
final=[];
$.each(html, function(index, value) {
    final.push($(value).html());
}); 

console.log(final.join(''));
content='在此添加图像描述在此添加图像描述在此添加图像描述在此添加图像描述在此添加图像描述';
html=$(内容);
$('img',html)。每个(函数(){
$(此).wrap(“”);
}); 
最终=[];
$.each(html、函数(索引、值){
final.push($(value.html());
}); 
控制台日志(final.join(“”));
所以,简单地将图像包装到$content中,然后遍历对象,从value生成jQuery对象,以获得html(),并输出最终字符串


演示>

很抱歉,这不起作用。返回的$content.html()将只有1个image@JeremyNelson上面的代码片段应该在
$中。每个
块是的,我正在查找$(此)。html始终为空jQuery方法不会处理字符串,但是,如果可以将()字符串附加到正文、一个隐藏元素和简单的wrap()图像,您可以轻松地获得修改后的html()并返回字符串?(如果我完全理解你的请求,不管怎样,我也不确定上下文。)@不管你是否理解这个请求,是的,这可能是这里的问题。变量$content=$(content);不是创建一个可以操作的dom元素吗?@Jermy Nelson,它创建了jQuery对象,是的,但不是dom元素(我想)…这里是(可能是)解决方法:无论如何,我想我得到了,你是对的,实现是错的。。。