使用 HTML、CSS 和/或 javascript 自动从图像源 (src=" ") 的链接中生成快照

发布于 2024-12-25 03:24:58 字数 406 浏览 3 评论 0 原文

在我的 HTML 文档中,我想为图像创建一个占位符,但保留源“待定”,这样当我在图像上放置链接时,它将从目标网站获取快照以用作图像源。如果您不太明白我在说什么,如下所示:

我想创建一个链接图像

并且我想使用 javascript 将“源”替换为“#”页面的快照。

我想使用它,以便在我的网站上我可以链接到 Youtube 视频(使用嵌入代码中的链接)并自动获取链接的缩略图,而无需输入链接/URL 之外的任何工作。

我不太懂 JavaScript,所以任何有关该部分的帮助将不胜感激,尽管如果可能的话,我会尝试使用非常少的 JavaScript 来完成此操作。非常感谢所有答案,如果需要更多信息,请询问。

In my HTML document I want to create a placeholder for an image but leave the source 'To be determined' so to speak so that when I put a link on the image it will acquire a snapshot from the target website to use as the image source. If you don't quite understand what I'm saying it is as follows:

I want to create a linked Image

<a href="#"><img src="source"></a>

and I want to use javascript to replace the 'source' with a snapshot of the '#' page.

I would like to use this so that on my website I can link to Youtube videos (using the link in the embed codes) and automatically acquire a thumbnail for the link without any work more than inputting the link/URL.

I am not very javascript savy so any help with that portion will be much appreciated, although I am trying to do this with very minimal Javascript if possible. All answers are much appreciated and if any more information is needed just ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2025-01-01 03:24:58

如果你想放置 YouTube 屏幕截图,我建议使用 jQuery 和 jYouTube 以及我如何将其组合在一起:

JAVASCRIPT:

// Run when page is load
$(function(){

    // Find all <a> inside element with youTube class name
    $(".youTube a").each(function(){

        // Get reference to found <a> link
        var lnk = $(this);

        // Get YouTube thumb image for <a>'s href attribute
        var url = $.jYoutube(lnk.attr("href"));

        // Now update inside image's src attribute with thumbs image
        lnk.children('img').attr("src", url);
    });
});

HTML

<div class="youTube">
<a href="http://www.youtube.com/watch?v=jZxRWTz8qiY"><img src="#" /></a><br />
<a href="http://www.youtube.com/watch?v=jZxRWTz8qiY"><img src="#" /></a><br />
<a href="http://www.youtube.com/watch?v=jZxRWTz8qiY"><img src="#" /></a><br />
</div>

另外,我将其放在 jsfiddle 中以方便演示: http://jsfiddle.net/snyew/

我希望这有帮助:-)

If you want to put YouTube screenshot I recommend using jQuery with jYouTube and here how I put it together:

JAVASCRIPT:

// Run when page is load
$(function(){

    // Find all <a> inside element with youTube class name
    $(".youTube a").each(function(){

        // Get reference to found <a> link
        var lnk = $(this);

        // Get YouTube thumb image for <a>'s href attribute
        var url = $.jYoutube(lnk.attr("href"));

        // Now update inside image's src attribute with thumbs image
        lnk.children('img').attr("src", url);
    });
});

HTML

<div class="youTube">
<a href="http://www.youtube.com/watch?v=jZxRWTz8qiY"><img src="#" /></a><br />
<a href="http://www.youtube.com/watch?v=jZxRWTz8qiY"><img src="#" /></a><br />
<a href="http://www.youtube.com/watch?v=jZxRWTz8qiY"><img src="#" /></a><br />
</div>

Also I put it in jsfiddle for easy demo: http://jsfiddle.net/snyew/

I hope this helps :-)

べ繥欢鉨o。 2025-01-01 03:24:58

它需要一个 Image 对象。

yourImg = document.getElementById("image_id");
var newImg = new Image();
newImg.src = //URL of the image to be acquired
yourImg.src = newImg.src;

It takes an Image object.

yourImg = document.getElementById("image_id");
var newImg = new Image();
newImg.src = //URL of the image to be acquired
yourImg.src = newImg.src;
甜味拾荒者 2025-01-01 03:24:58

假设您想对每个仅包含图像作为子项的链接执行此操作,您可以这样做:

$('a').each(function() {
  if($(this).children('img').length == 1 && $(this).children().length == 1) {

    // get snapshot
    var snapshotImgURL = getSnapShotOf($(this).attr('href')); // replace this with your AJAX call to get the snapshot

    // set it to the image
    $(this).children('img').attr('src', snapshotImgURL);

  }
});

上面假设您可以在项目中使用 jQuery。

Assuming you want to do this for every link with just an image as child, you can do:

$('a').each(function() {
  if($(this).children('img').length == 1 && $(this).children().length == 1) {

    // get snapshot
    var snapshotImgURL = getSnapShotOf($(this).attr('href')); // replace this with your AJAX call to get the snapshot

    // set it to the image
    $(this).children('img').attr('src', snapshotImgURL);

  }
});

The above assumes you are ok with using jQuery on the project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文