用php添加目标和rel

时间:2017-06-01 00:58:41

标签: php target rel

想象一下你有这个标记:

原始代码:

<p class="Add">
  <a href="https://google.com">Google</a>
  <a href="https://google.com">Google</a>
  <a href="https://google.com">Google</a>
  <a href="https://google.com">Google</a>
</p>

所需代码:

<p class="Add">
  <a href="https://google.com" target="_blank" rel="noFollow noReferrer" class="l">Google</a>
  <a href="https://google.com" target="_blank" rel="noFollow noReferrer" class="l">Google</a>
  <a href="https://google.com" target="_blank" rel="noFollow noReferrer" class="l">Google</a>
  <a href="https://google.com" target="_blank" rel="noFollow noReferrer" class="l">Google</a>
</p>

针对&#34; p&#34;与班级#34;添加&#34;,如何用php添加:

  • &#34;目标&#34;所有嵌套元素的属性
  • &#34; rel&#34;所有嵌套元素的属性
  • 班级&#34; l&#34;到所有嵌套元素

我需要动态更改html结构,原始链接由模块构建,在构造原始html之前我无法更改它们。在我使用javascript之前,但是js不是一个选项,因为有些用户没有在浏览器中启用j。

我以前的js代码是:

$(".Add a").attr("target", "_blank");
$(".Add a").attr("rel", "noFollow noReferrer");
$(".Add a").addClass("l");

谢谢。

1 个答案:

答案 0 :(得分:0)

这是你正在寻找的吗?你可以这样做,只有在满足某个条件时才会打印。

<p class="Add">
    <a href='https://google.com' <?php echo("target='_blank'") ?> <?php echo("rel='noFollow noReferrer'") ?> <?php echo("class='l'") ?>>Google</a>
    <a href='https://google.com' <?php echo("target='_blank'") ?> <?php echo("rel='noFollow noReferrer'") ?> <?php echo("class='l'") ?>>Google</a>
    <a href='https://google.com' <?php echo("target='_blank'") ?> <?php echo("rel='noFollow noReferrer'") ?> <?php echo("class='l'") ?>>Google</a>
    <a href='https://google.com' <?php echo("target='_blank'") ?> <?php echo("rel='noFollow noReferrer'") ?> <?php echo("class='l'") ?>>Google</a>
</p>