简体   繁体   English

在javascript中加载带有src的img对象

[英]Loading an img object with a src in javascript

I want to add a thumbnail picture to a book's details, derived from the google books api, on the webpage.我想在网页上从谷歌图书 api 派生的书籍详细信息中添加缩略图。 The code below will place the source code (api) for the appropriate book, first into the text field bookCover and then into the var copyPic, and then it should be copied into imgDisp, but it doesn't.下面的代码将放置相应书籍的源代码 (api),首先放入文本字​​段 bookCover 中,然后放入 var copyPic 中,然后应该将其复制到 imgDisp 中,但它没有。 I can see that bookCover holds the right text, and have checked that copyPic holds the correct content.我可以看到 bookCover 包含正确的文本,并检查了 copyPic 包含正确的内容。

<img id="imgDisp" src="http://books.google.com/books/content? 
id=YIx0ngEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api" width="85" height="110"" />

$.getJSON(googleAPI, function(response) {
    $("#title").html(response.items[0].volumeInfo.title);
    $("#subtitle").html(response.items[0].volumeInfo.subtitle);
    $("#author").html(response.items[0].volumeInfo.authors[0]);
    $("#description").html(response.items[0].volumeInfo.description);
    $("#version").html(response.items[0].volumeInfo.contentVersion);
    $("#modeR").html(response.items[0].volumeInfo.readingModes.text);
    $("#bookCover").html(response.items[0].volumeInfo.imageLinks.thumbnail);

    var copyPic = document.getElementById('bookCover').innerHTML;
    document.getElementById("imgDisp").src=copyPic;

Does anyone know why not?有谁知道为什么不? Or can I put the api details directly into imgDisp (can't find such code syntax anywhere on the net)?或者我可以将api详细信息直接放入imgDisp(在网上的任何地方都找不到这样的代码语法)? Everything else is working fine.其他一切正常。 If I put a src in directly, then it works eg如果我直接放入一个 src ,那么它就可以工作,例如

document.getElementById("imgDisp").src = “http://.....api” 

but not with a variable.但不是带有变量。

Without more info - eg, I can't see where the getJSON() function ends or what the URL's are, I can't see what the issue may be (except, perhaps, as in my last comment).没有更多信息 - 例如,我看不到 getJSON() 函数在哪里结束或 URL 是什么,我看不到问题可能是什么(除了,也许,就像我上次评论一样)。

I idea seems ok, as I can replicate it (in a cut-down version of course):我的想法似乎没问题,因为我可以复制它(当然是精简版):

 function copyImageSource() { let d = document.getElementById("bookCover").innerHTML; document.getElementById("imgDisp").src = d; }
 <button onclick="copyImageSource();">Get image</button> <div id="bookCover">https://duckduckgo.com/assets/icons/meta/DDG-icon_256x256.png</div> <img id="imgDisp" src="">

I assume that this is the sort of thing you are trying to achieve?我认为这是您想要实现的目标?

(javascript -> jquery: (javascript -> jquery:

let copyPic = $("#bookCover").html();
$("#imgDisp").attr("src", copyPic);

) )

Version using jquery:使用 jquery 的版本:

 function copyImageSource() { let d = $("#bookCover"); d.html("http://books.google.com/books/content?id=YIx0ngEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api"); let dCopy = d.html().replace(/&amp;/g, "&"); $("#imgDisp").attr("src", dCopy); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="copyImageSource();">Get image</button> <div id="bookCover"></div> <img id="imgDisp" src="https://www.picsearch.com/images/logo.png"/>

If you have jQuery you can easily do the following:如果您有 jQuery,您可以轻松地执行以下操作:

let source = 'https://img.com/image.png';

//to get the image object that has the above just do this:
let img = $('img[src="' + source + '"]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM