Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.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 查找文章中所有图像的图像src url_Javascript_Jquery_Image_Jekyll_Lightbox2 - Fatal编程技术网

Javascript 查找文章中所有图像的图像src url

Javascript 查找文章中所有图像的图像src url,javascript,jquery,image,jekyll,lightbox2,Javascript,Jquery,Image,Jekyll,Lightbox2,简短总结:我现在的处境很糟糕,我需要找到每个图像的src url并将其发布回a href=“imgurl”。jQuery或Javascript 到目前为止,我尝试的是: $(document).ready(function(){ var postTitle = document.title.replace(/ /g,''); $("article img").each(function() { imgsrc = this.src; console.l

简短总结:我现在的处境很糟糕,我需要找到每个图像的src url并将其发布回a href=“imgurl”。jQuery或Javascript

到目前为止,我尝试的是:

$(document).ready(function(){

    var postTitle = document.title.replace(/ /g,'');   

    $("article img").each(function() {  
    imgsrc = this.src;
    console.log(imgsrc);
    });

    $("article img").wrap('<a href="' + imgsrc + '" data-lightbox="' + postTitle + '"></a>');
$(文档).ready(函数(){
var postTitle=document.title.replace(//g',);
$(“article img”).each(function(){
imgsrc=this.src;
控制台日志(imgsrc);
});
$(“物品img”)。包装(“”);
我之所以将文章img包装在a-tag中,是因为我用于标记的pose.io会自动分配p-tags中的图像

我最终想做的是将同一帖子(data lightbox)中的所有图像分组,并从添加到页面的图像自动创建lightbox库

使用我现有的代码,我成功地从第一个添加的图像获取了url,但不幸的是,帖子中的所有图像都被分配了与第一个图像相同的url。它们在站点上显示为正确的图像,但url与第一个图像完全相同(数组中的位置[0])。当我单击图像时,lightbox将打开,并显示正确数量的图像,但它们都显示第一个图像。console.log将正确打印文章中图像的所有URL

编辑-奖金:已验证!作为奖金问题:如果我对alt文本执行相同的操作,将其复制到数据标题属性中,我将如何继续?目前有以下问题:

var altText = $("img", this).attr("alt");

    $("article img").each(function() {
    $(this).wrap('<a href="' + this.src + '" data-lightbox="' + postTitle + '" data-title="' + altText + '"></a>');
var altText=$(“img”,this.attr(“alt”);
$(“article img”)。每个(函数(){
$(此).wrap(“”);
}))

同样,它只会获取第一个图像alt,并将其分配给所有图像

我希望你们中的一些人知道如何摆脱这种混乱


提前感谢您的时间!

每次有图像时,您都将覆盖
imgsrc

试着这样做:

$("article img").each(function() {
    var src = this.src;
    var alt = $(this).attr("alt");
    $(this).wrap('<a href="' + src + '" data-lightbox="' + postTitle + '" data-title="' + alt + '"></a>');
});
$(“文章img”)。每个(函数(){
var src=this.src;
var alt=$(this.attr(“alt”);
$(此).wrap(“”);
});

查看更多信息!

每次有图像时,您都会覆盖
imgsrc

试着这样做:

$("article img").each(function() {
    var src = this.src;
    var alt = $(this).attr("alt");
    $(this).wrap('<a href="' + src + '" data-lightbox="' + postTitle + '" data-title="' + alt + '"></a>');
});
$(“文章img”)。每个(函数(){
var src=this.src;
var alt=$(this.attr(“alt”);
$(此).wrap(“”);
});

查看更多信息!

非常感谢您的回答。这很有魅力!同时,感谢您指出我做错了什么;这使学习变得更加容易。很高兴提供帮助!不要忘记验证答案!验证!另外,请检查奖励问题:)太棒了!非常感谢你,老兄!非常感谢你的回答。这很有魅力!同时,感谢你指出我做错了什么;这让学习变得更容易。很高兴帮助你!别忘了验证答案!验证!另外,请检查奖励问题:)太棒了!非常感谢你,老兄!