简体   繁体   中英

javascript - get img src

If I have an img in index.html :

<script src="script/script.js" type="text/javascript"></script>
<img id="imgb" src="picture/banner1.jpg" border="0">

then in a separate file, such as gallery.html , I need to get the src element of the img in index.html .

I tried writing this function:

function imgbbs(){
    var x = document.getElementById('imgb').src;
    document.getElementById('imgbb').src = x;
}

Then try using it in gallery.html :

<script src="script/script.js" type="text/javascript"></script>
<body onload="imgbbs()">
    <img id="imgbb" src="" border="0">

But it doesn't seem to be working. How can I get the src attribute of an img in a different file?

一种方法是使用jquery加载index页面的一部分,就像这样:

$( "#your_image_container_in_gallery.html" ).load( "index.html #imgb" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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