使用jQuery隐藏div onClick()中的img标记

时间:2014-09-02 08:58:40

标签: javascript php jquery html css

我试图在下面的代码中显示和隐藏img标记

<div id='userview_'<?php echo $userId; ?>>
    <img src="css/user/images/user1.svg">
</div>

我写了jquery来展示像im

这样的img
var userId='<?php echo $userId; ?>';
jQuery('#userview_'+userId ' img').css('src','url(css/user/images/user1.svg) no repeat center');

但没有工作

我还需要知道在onClick()

中隐藏这个img标签(仅)

6 个答案:

答案 0 :(得分:1)

Src是一个属性,因此您需要执行类似下面的操作并提供正确的图像路径,

var userId='<?php echo $userId; ?>';
jQuery('#userview_'+userId ' img').attr('src','css/user/images/user1.svg');

答案 1 :(得分:0)

jQuery('#userview_'+userId +' img')

                          ^^^^ you messed    

使用 attr

HTML

<div id='userview_33'>
    <img src="css/user/images/user1.svg" />
</div>

JS

var userId = 33;

jQuery('#userview_' + userId +' img').attr('src', 'url(css/user/images/user3.svg) no repeat center'); 

答案 2 :(得分:0)

首先在你的html中将php标签放在''这样的<div id='userview_<?php echo $userId; ?>'>

里面

然后你可以做这样的事情

jQuery('#userview_'+userId+' img').click(function(){
    jQuery(this).hide();
});

答案 3 :(得分:0)

你拥有的地方:

var userId='< ? php echo $userId; ?>';

jQuery('#userview_'+userId ' img').css('src','url(css/user/images/user1.svg) no repeat center');

请记住,PHP脚本仅适用于PHP文件。你可以这样做: index.php的一部分

<div class="imgContainer"></div>
...
<script>var userId = <?php userId?> </script>

并在JS文件中:

jQuery('#userview_'+userId +' .imgContainer').css('src','url(css/user/images/user1.svg) no repeat center');

如果你想点击活动:

jQuery('#userview_'+userId ' imgContainer').on('click', function(){
   /*some code*/
});

也可以通过本教程:http://try.jquery.com/levels/1/challenges/1

答案 4 :(得分:0)

<div id='userview_'<?php echo $userId; ?>>
    <img src="css/user/images/user1.svg" />
</div>
<a href="#" class="testclick">Click</a>
$('.testclick').click(function(){
var userId='<?php echo $userId; ?>';
    $('#userview_'+userId+' img').hide();
});

答案 5 :(得分:0)

最后我发现自己是一个方法

 jQuery('#userview_'+userId , "'<img src=css/user/images/user1.svg>'").remove();

当我需要展示它时,我使用了

jQuery('#userview_'+userId).append('<img src="css/user/images/user1.svg>">');

谢谢