How to get a img of uppper leve a tag href?
I need to change my content img src.
So, I want to get the uppper leve a tag href first.
In my limited experience with that
Now Code
<a href="data/abc.png"><img src="test.png"></a>
I want change to
<a href="data/abc.png"><img src="data/abc.png"></a>
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
any suggest? should me neef to use preg_replace?
Using jQuery you can do like below:
$(document).ready(function() {
$('img').attr('src', $('img').parent('a').attr('href'));
});
Working snippet:-
$(document).ready(function() { $('img').attr('src', $('img').parent('a').attr('href')); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="data/abc.png"> <img src="test.png"> </a>
Note:- if your document have multiple <a>
with <img>
in it, above code will change src
of all.So be careful.
In PHP (update your code like below):
<a href="<?php php code to get href value ?>">
<img src="<? write the same php code to get href value ?>">
</a>
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.