必须通过点击链接传递图像名称,图像应出现在另一个div中

时间:2013-04-26 05:04:49

标签: jquery cakephp cakephp-2.0

我是这个网络领域的新手。 我遇到了一个问题...... 我有一个div中的链接列表,如果我点击该特定链接,与该链接相关的图像应该出现在另一个div中。我正在从数据库中检索的图像的名称,因此它应该传递给该div,并且应该在该div中看到该图像。

我看到很多与jquery和javascript有关的例子,但没有找到特定的例子。因为我很新,所以我不善于按照我的要求编辑和修复这些例子。 需要帮助。

使用cakephp框架进行开发

此处创建了所有链接

<?php foreach($news as $count): ?>
<div class="title">
        <a href="#"> <?php echo $count['News']['title'];?> </a> 

</div>
 

在这个foreach之外有一个div

<div id="vedio-image">
          <?php // $this->Html->image($count['New']['vedioImage']); ?>
</div>

如何在循环外使用$ count ['News'] ['vedioImage']并将其传递给jquery

1 个答案:

答案 0 :(得分:0)

根据我的理解,你有像这样的HTML。

<?php foreach($news as $count): ?>
<div class="title"> <a href="#" rel="<?php echo $count['News']['image'];?>"> <?php echo $count['News']['title'];?> </a> </div>
<?php endforeach;?>


<!--Another div to show image -->
<div id="vedio-image"></div>

你可以做的是将图像路径添加到锚点(a)rel属性中。

现在来了jQuery

$('div.title a').click(function(){ 
    $('#vedio-image').html('<img src="' + $(this).attr('rel') + '" />'); 
});

如果你在标题中编写javascript,只需在准备函数中包含上面的代码。

$(document).ready(function(){
    $('#div.title a').click(function(){ 
        $('#vedio-image').html('<img src="' + $(this).attr('rel') + '" />'); 
    });
});

希望这会有所帮助,否则就会提供有关您代码的更多信息。