简体   繁体   中英

HTML concatenating a span within img tag

I have an image tag in html which was previously referencing an image from a php array. I have recently updated this to a javascript object so I can use an AJAX request

<img src="http://www.example.com/images/<?php echo $arrayElement[item]; ?>.gif">

<script>
    document.getElementById('item').innerHTML = this.processingData['item'];
</script>

I have tried:

<img src="http://www.example.com/images/<?php echo "<span id='item'></span>"; ?>.gif">

and the end result I want is:

<img src="http://www.example.com/images/[nameOfImage].gif">

of what i understand there is no need to put html tags in there. what you need to echo in there is pure and simple the image name

<img src="http://www.example.com/images/<?php echo $arrayElement[item]; ?>.gif">

was correct. now if you need to affect the src attribute in javascript, simply give it a handle (an id would work if the image is unique within it's page) like this

<img id="myImage" src="http://www.example.com/images/<?php echo $arrayElement[item]; ?>.gif">

and then refer to it in javascript

var img = document.getElementsById("myImage");
img.src = "anyValidImgUrl";

if you get your image url from the ajax query, use the upper code snippet in your ajax callback. if you are using jquery, chainging the image url is done as follows

$(".myImage").attr("src","anyValidImgUrl");

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