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>

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

4

1 回答 1

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') + '" />'); 
    });
});

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

于 2013-04-26T05:17:16.920 回答