php:将 "alt tag" 内容回显到 "title tag"

php: Echo "alt tag" contents to "title tag"

PHP 文件中的图像有一个 "alt" 标签,但我需要将其内容复制到 "title" 标签。因此代码在加载时自动读取页面,并在 "alt" 标签旁边添加新的 "title" 标签,无论使用 PHP、jQuery、JavaScript。 .

示例:

<img border="0" src="../../../../images/divider.jpg" alt="description of image" width="410" height="15">

输出:

<img border="0" src="../../../../images/divider.jpg" alt="description of image" title="description of image" width="410" height="15">

使用JavaScript,您可以搜索<img>个标签,并添加这个title属性:

let imgs = document.querySelectorAll('img');
for (let i=0;i<imgs.length;i++) {
  if (!imgs[i].title) // if title is not defined
      imgs[i].title = imgs[i].alt;
}

使用 jQuery,您可以使用查找图像 $('img') 并使用 each() 循环:

$('img').each(function(img) {
  if (!this.title)
    this.title = this.alt;
});