简体   繁体   中英

Setting 'src' of img tag dynamically using backbone templating

I am trying to set the src of img tag dynamically with backbone templating, but / is appended automatically for which img not loaded.The part of template looks like -

<script type="text/template" id="home">
.......
<% var d=names[0].get("image_link")+".jpg" 
//d=d.substring(0 , d.length-1); tried to cut down last char
%>
<a href="#" class="thumbnail"><img class="img-responsive ui-corner-all"  width="100%" height="auto" src=<%=d%> /> 
.......
</script>

The output -

<a href="#" class="thumbnail"><img class="img-responsive ui-corner-all"  width="100%" height="auto" src="image1.jpg/" />

I couldn't figure it out how to exclude that / in img src. Any suggestion please.

That slash is actually there in the code. As you don't have any quotation marks around the attribute value, the / in /> becomes part of the value.

Add quotation marks:

<a href="#" class="thumbnail"><img class="img-responsive ui-corner-all"  width="100%" height="auto" src="<%=d%>" />

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