简体   繁体   中英

How to wrap link around image properly?

I have been trying to wrap a link around an image. I have searched and found that wrap() in jQuery would help me. When I tried it, nothing seems to work. Is there something that I am doing wrong?

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img src="http://a1.mzstatic.com/us/r30/Music3/v4/fb/af/59/fbaf5908-0839-abc6-9f6a-bc7cc5b84f27/cover170x170.jpeg" class="YTimgs"> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript' /> <script> $('.YTimgs').wrap('<a href="http://www.chordzone.org"></a>'); </script> 

Your code seems to be ok as it works in this fiddle http://jsfiddle.net/pqqyoakm/ It could be that the javascript is executing before the image has loaded try:

$(document).ready(function(){
    $(".YTimgs").wrap("<a href='http://www.chordzone.org'></a>'")
})

The problem is that you are missing the closing tag for the script that loads jQuery, so the script tag after it will be swallowed up and ignored.

A script tag can not be self-closed, so the second script tag ends up part of the first one, and as there is a src attribute in the script tag, the content inside the tag is ignored.

Add an ending tag to the script tag:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>

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