在图像下方和h1 html标记中显示图像alt或标题

时间:2014-06-02 21:24:53

标签: php jquery html image magento

我在网站上有一个图片轮播。当我做动子时,我想在图像下面显示图像alt标签或标题。同时我想在另一个h1标签中显示相同的名称。

代码在PHP和Magento实现中。我知道这是一个简单的伎俩。但我尝试使用jQuery之后的所有代码,追加和替换函数。这些功能正常运作。但我无法显示alt或标题名称。

Php代码:

<ul id="mycarousel" class="jcarousel-skin-tango alt">
<?php 
// products 
if(!isset($CatID))
$CatID = 10; // Set to classic collection if empty;
$_category = Mage::getModel('catalog/category')->load($CatID);
$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('status', 1)//enabled
        ->addCategoryFilter($_category);
foreach($products as $index=>$prod):
$product = Mage::getModel('catalog/product')->load($prod->getId());
echo '<li style="border: none;">
<a class="thumbActImg1" href="'.$product->getProductUrl().'" data-rel="'.(string)Mage::helper('catalog/image')->init($product, 'small_image')->keepFrame(FALSE)->resize($_resize, $_resize).'" alt="'. $product->getName() .'">
<div class="thumbActImg2">
<img src="'.$product->getThumbnailUrl(80, 80).'" alt="'. $product->getName() .'" />        
</div>
</a>
</li>';
endforeach;
?>
</ul>

jQuery代码:

jQuery(this,'img.thumbActImg2').after('<div class="pname"><?php echo     $product->getName()?></div>');
jQuery('#pro_name h1').replaceWith('<h1><?php echo $product->getName()?></h1>');

现在,我使用php echo来显示名称。但它显示当前页面产品名称而不是图像名称上的移动设备。

有人可以帮帮我吗。

1 个答案:

答案 0 :(得分:1)

<强>您好,

在生成站点时,将对PHP代码进行一次评估。因此<h1><?php echo $product->getName()?></h1>始终是相同的文字。

当图像循环时(如果我理解你),你必须重写/获取图像中的当前信息。所以这可能对你有用:

//insert the code, where the carousel is triggered
//get the alt attribute from image and replace h1 content
jQuery('#pro_name h1').html(jQuery('img.thumbActImg2').attr('alt'));

我希望这是您正在寻找的内容,这个问题不会非常简短地进行描述。

问候席