【发布时间】:2015-07-24 08:31:22
【问题描述】:
我有一组 alt 标签:
$array_alt=array("hii,
"Best",
"abc",
//"board certified",
"xyz",
"hello",
"new"
)
现在我想要将这些 alt 标签添加到 img src 动态,即 image 1 alt 标记应该是 "hii",其余标记相同。
这就是通过文件夹动态调用图像并且所有图像都显示在滑块中的方式:
$images = scandir(images/all_images); // get path
sort($images,1); // 1 is to sort images numerically
foreach($images as $img)
{
if($img === '.' || $img === '..')
{
continue;
}
// check extensions as we need only images
if ((preg_match('/.jpg/',$img)) || (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )
{
list($width, $height, $type, $attr) = getimagesize($path.$img);
if(($width<$height) || ($height>500) )
{
?>
<div class="item img-landscape"><img class="lazyOwl" alt="" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
else{
?>
<div class="item"><img class="lazyOwl" alt="" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
}
else
{
continue;
}
}
如果有人可以帮助我,请。
我尝试使用它,但似乎没有任何效果。
$images = scandir($img_path);
sort($images,1); // 1 is to sort images numerically
$i=0;
foreach($images as $img)
{
echo $array_alt[$i]."<br>";
if($img === '.' || $img === '..')
{
continue;
}
// check extensions as we need only images
if ((preg_match('/.jpg/',$img)) || (preg_match('/.gif/',$img)) || (preg_match('/.tiff/',$img)) || (preg_match('/.png/',$img)) )
{
list($width, $height, $type, $attr) = getimagesize($path.$img);
if(($width<$height) || ($height>500) )
{
?>
<div class="item img-landscape"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
else{
?>
<div class="item"><img class="lazyOwl" alt="<?php echo $array_alt[$i]?>" data-src="<?php echo $path.$img; ?>" ></div>
<?php
}
}
else
{
continue;
}
$i++;
}
提前致谢
【问题讨论】:
-
为什么是 JavaScript 和 jQuery 标签?
-
您的代码显示什么错误?你的alt标签没变吗?问题是什么 ?欢迎提供更多信息:)
-
如果这可以使用 javascript 或 jquery 完成,这就是为什么还要添加 thsee
-
我尝试的代码多次打印相同的alt标签,这是没有用的。我希望每个图像都应该有自己的 alt 标签,如数组中定义的那样
-
你确定你的匹配规则是正确的吗?如果您说多次使用相同的 alt 标签,那么我可以看到您的代码为什么不起作用的唯一方法是您的代码将继续;声明过于频繁。
标签: javascript php arrays