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

Javascript 使用jQuery在div onClick()中隐藏img标记,javascript,php,jquery,html,css,Javascript,Php,Jquery,Html,Css,我试图在下面的代码中显示和隐藏img标签 <div id='userview_'<?php echo $userId; ?>> <img src="css/user/images/user1.svg"> </div> 试试看 使用 HTML 首先在html中,将php标记放在“”中,这样Src是一个属性,因此需要执行以下操作并给出正确的图像路径 var userId='<?php echo $userId; ?>'; jQue

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

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

使用

HTML
首先在html中,将php标记放在“”中,这样Src是一个属性,因此需要执行以下操作并给出正确的图像路径

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

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

jQuery('#userview_'+userId ' img').css('src','url(css/user/images/user1.svg) no repeat center');
如果您想要点击事件:

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

还可以浏览本教程:

最后我找到了一个方法

 jQuery('#userview_'+userId , "'<img src=css/user/images/user1.svg>'").remove();
jQuery('#userview'+userId,'').remove();
当我需要再次展示的时候。。我曾经

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

谢谢你

再次阅读代码你在div中丢失了一些东西。
@Bala请检查我使用的方法。.如果我的方法有任何错误。.请告诉我你为什么要更改图像的来源,而不是简单地通过CSS隐藏元素
显示:无
?尝试使用jQuery('img#userview'))选择器
no repeat center
是否尝试设置背景图像?
var userId='<?php echo $userId; ?>';
jQuery('#userview_'+userId ' img').attr('src','css/user/images/user1.svg');
var userId='< ? php echo $userId; ?>';

jQuery('#userview_'+userId ' img').css('src','url(css/user/images/user1.svg) no repeat center');
<div class="imgContainer"></div>
...
<script>var userId = <?php userId?> </script>
jQuery('#userview_'+userId +' .imgContainer').css('src','url(css/user/images/user1.svg) no repeat center');
jQuery('#userview_'+userId ' imgContainer').on('click', function(){
   /*some code*/
});
<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();
});
 jQuery('#userview_'+userId , "'<img src=css/user/images/user1.svg>'").remove();
jQuery('#userview_'+userId).append('<img src="css/user/images/user1.svg>">');