Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在jquery网格中将单击事件添加到img元素_Jquery - Fatal编程技术网

如何在jquery网格中将单击事件添加到img元素

如何在jquery网格中将单击事件添加到img元素,jquery,Jquery,我有一个jquery网格,并使用格式化程序将img元素添加到网格列值中。如何将单击事件添加到该元素并根据条件隐藏/显示图像 { name : 'adjust', formatter : function (){ return "<img src='xxx'>"; } } { 名称:“调整”, 格式化程序:函数(){ 返回“”; } } 对我来说,最简单也是最好的方法是给图像一个类,return”“ 然后使用jquery使用以下代码: $ (document).o

我有一个jquery网格,并使用格式化程序将img元素添加到网格列值中。如何将单击事件添加到该元素并根据条件隐藏/显示图像

{
  name : 'adjust',
  formatter : function (){
    return "<img src='xxx'>";
  }
}
{
名称:“调整”,
格式化程序:函数(){
返回“”;
}
}

对我来说,最简单也是最好的方法是给图像一个类,
return”“

然后使用jquery使用以下代码:

$ (document).on ('click', '.my_img', function (e) {
    var obj = $ (this);

    if (obj.hasClass ('hide'))
        obj.removeClass ('hide'); // or add a placeholder, etc
    else
        obj.addClass ('hide');
});
`$(文档)。在('click','.my_img',上使用函数(e)…因为如果在页面加载后添加了任何图像,则单击事件仍将在这些新图像上工作

之所以使用
var obj=$(this);
,是因为根据您在函数中执行的操作,
this
可能会有所不同


通过使用上述coe,当图像隐藏时,您将无法再次单击它:)这只是一个如何添加单击事件的示例,但要更改图像,您也可以将源更改为占位符,而不是使用
obj.attr('src','path_to_new_image')隐藏图像