简体   繁体   English

PHP获取上层A Tag href链接的IMG Tag?

[英]PHP get the IMG Tag of upper level A Tag href link?

How to get a img of uppper leve a tag href?如何获得上层标签href的img?

I need to change my content img src.我需要更改我的内容 img src。

So, I want to get the uppper leve a tag href first.所以,我想先让上层有一个标签 href。

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?我应该使用 preg_replace 吗?

Using jQuery you can do like below:使用 jQuery,您可以执行以下操作:

$(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.注意:- 如果你的文档中有多个<a><img> ,上面的代码会改变所有的src 。所以要小心。

In PHP (update your code like below):在 PHP 中(更新您的代码,如下所示):

<a href="<?php php code to get href value ?>">
  <img src="<? write the same php code to get href value ?>">
</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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