Javascript 使用jQuery在单击时更改图像

Javascript 使用jQuery在单击时更改图像,javascript,jquery,Javascript,Jquery,很抱歉标题含糊不清,但我真的不知道该如何描述 我要做的是再次单击,更改图像。下面是代码的这一部分 有人能告诉我这是否可行吗?我没有看到这样做的方法,因为我不确定如何引用锚标记 $(document.createElement('a')) .attr('href', '#') .html('<img src="myimage.jpg" />') .click( function( e ){ // When the user clicks the anchor tag

很抱歉标题含糊不清,但我真的不知道该如何描述

我要做的是再次单击,更改图像。下面是代码的这一部分

有人能告诉我这是否可行吗?我没有看到这样做的方法,因为我不确定如何引用锚标记

$(document.createElement('a'))
  .attr('href', '#')
  .html('<img src="myimage.jpg" />')
  .click( function( e ){
    // When the user clicks the anchor tag above, I want to replace the current
    // image (myimage.jpg) with another one.
  }
$(document.createElement('a'))
.attr('href','#')
.html(“”)
。单击(功能(e){
//当用户单击上面的锚标记时,我想替换当前
//图像(myimage.jpg)与另一个图像。
}

假设您希望保留编写代码时的代码状态(这很糟糕),
e.currentTarget
是对正在单击的元素的引用。您可以使用该引用对另一个图像URL重复(不推荐的)
.html()
过程

$(document.createElement('a'))
    .attr('href', '#')
    .html('<img src="myimage.jpg" />')
    .click( function( e ){
        $(e.currentTarget).html('<img src="myimage2.jpg" />');
    });
$(document.createElement('a'))
.attr('href','#')
.html(“”)
。单击(功能(e){
$(e.currentTarget).html(“”);
});

假设您希望保留编写代码时的代码状态(这很糟糕),
e.currentTarget
是对正在单击的元素的引用。您可以使用该引用对另一个图像URL重复(不推荐的)
.html()
过程

$(document.createElement('a'))
    .attr('href', '#')
    .html('<img src="myimage.jpg" />')
    .click( function( e ){
        $(e.currentTarget).html('<img src="myimage2.jpg" />');
    });
$(document.createElement('a'))
.attr('href','#')
.html(“”)
。单击(功能(e){
$(e.currentTarget).html(“”);
});

最简单的方法就是给映像一个类,比如“myImg”,然后使用
.on()
方法绑定到动态创建的元素

$(document).on('click', '.myImg', function(){
    //$(this) refers to the image that we clicked
    $(this).prop('src', 'http://my-domain.com/path/to/my/new-image.jpg');
});

这会将事件绑定到文档,并且每当单击具有“myImg”类的图像时,它都会替换该图像。

最简单的方法就是为图像指定一个类,如“myImg”,然后使用
.on()
方法绑定到动态创建的元素

$(document).on('click', '.myImg', function(){
    //$(this) refers to the image that we clicked
    $(this).prop('src', 'http://my-domain.com/path/to/my/new-image.jpg');
});
$(document.createElement('a'))
  .attr('href', '#')
  .html('<img src="myimage.jpg" />')
  .click( function( e ){
      $(this).find('img').attr('src','myNewImage.jpg');
  });
}
这会将事件绑定到文档,并且每当单击具有“myImg”类的图像时,它都会替换该图像。

$(document.createElement('a'))
$(document.createElement('a'))
  .attr('href', '#')
  .html('<img src="myimage.jpg" />')
  .click( function( e ){
      $(this).find('img').attr('src','myNewImage.jpg');
  });
}
.attr('href','#') .html(“”) 。单击(功能(e){ $(this.find('img').attr('src','myNewImage.jpg'); }); }
$(document.createElement('a'))
.attr('href','#')
.html(“”)
。单击(功能(e){
$(this.find('img').attr('src','myNewImage.jpg');
});
}
//创建节点
$('', {
“href”:“#”,
“src”:“myimage.jpg”
})
//把它加到身体上
.appendTo('正文')
//添加环境
.on('单击',函数()){
$(this.attr('src','otherimage.jpg');
//防止由“#”引起的跳转到页面顶部
返回false;
});
//创建节点
$('', {
“href”:“#”,
“src”:“myimage.jpg”
})
//把它加到身体上
.appendTo('正文')
//添加环境
.on('单击',函数()){
$(this.attr('src','otherimage.jpg');
//防止由“#”引起的跳转到页面顶部
返回false;
});
使用这个简单的

$('img').click(function () {
 $(this).attr('src', 'src/to/new/file.png');
}
如果要在单击位于
a
上方时更改图像,请使用

$('a').click( function () {
而不是图像


这将更改当前图像的src属性

这是一把小提琴:)

当鼠标悬停在图像上时,图像会发生变化。您可以使用
单击
而不是
悬停
。这应该行得通。如果必须使用
href=“#”
禁用它,请不要创建
a

如果您想要指针的光标,请在CSS中使用
cursor:pointer
,或者使用

$(this).css('cursor', 'pointer');
用这个简单的

$('img').click(function () {
 $(this).attr('src', 'src/to/new/file.png');
}
如果要在单击位于
a
上方时更改图像。然后使用

$('a').click( function () {
而不是图像


这将更改当前图像的src属性

这是一把小提琴:)

当鼠标悬停在图像上时,图像会发生变化。您可以使用
单击
而不是
悬停
。这应该行得通。如果必须使用
href=“#”
禁用它,请不要创建
a

如果您想要指针的光标,请在CSS中使用
cursor:pointer
,或者使用

$(this).css('cursor', 'pointer');

更改src属性您必须将其添加到DOM中,使其甚至可单击谢谢Scott。我可以做到,但我想我看到了一个问题。这个锚定标记位于一个表行的内部,这个网格中有许多行。我怎样才能正确识别正确的标签呢?现在我的评论毫无意义了…@Gavin,幸好我不是一个非常直率的人,或者我可能真的出去买了一套西装,以为这样可以解决我的问题。:)更改src属性您必须将其添加到DOM中,使其甚至可单击谢谢Scott。我可以做到,但我想我看到了一个问题。这个锚定标记位于一个表行的内部,这个网格中有许多行。我怎样才能正确识别正确的标签呢?现在我的评论毫无意义了…@Gavin,幸好我不是一个非常直率的人,或者我可能真的出去买了一套西装,以为这样可以解决我的问题。:)谢谢你,布莱恩。我很接近。我对DOM真的很陌生,当我浏览它时,我注意到了它,并认为我可以使用它。不过现在我知道你是怎么做的了,我完全错了。谢谢布莱恩。我很接近。我对DOM真的很陌生,当我浏览它时,我注意到了它,并认为我可以使用它。不过现在我知道你是怎么做的了,我完全错了。谢谢,为什么。我现在就试试这个谢谢,为什么。我现在就试试这是个好主意。我喜欢。我也要试试这个。谢谢你,这是个好主意。我喜欢。我也要试试这个。谢谢你