Jquery 如何使用来自父锚点的href设置图像src?
我有以下链接,它应该在加载javascript时显示链接中的图像:Jquery 如何使用来自父锚点的href设置图像src?,jquery,image,this,href,src,Jquery,Image,This,Href,Src,我有以下链接,它应该在加载javascript时显示链接中的图像: <a href="http://upload.wikimedia.org/wikipedia/commons/a/ae/Mad_scientist.svg" id="a"> <img alt="no image"> </a> javascript应该获取链接的href属性,并在为图像编写html时包含该属性 $('a').html('image'); 不知何故,$(this.attr
<a href="http://upload.wikimedia.org/wikipedia/commons/a/ae/Mad_scientist.svg" id="a">
<img alt="no image">
</a>
javascript应该获取链接的href属性,并在为图像编写html时包含该属性
$('a').html('image');
不知何故,$(this.attr('href')
返回了未定义的。这有什么问题
在上述代码中,此
指的是当前元素,即具有id=“a”
在上述代码中,此
指的是当前元素,即具有id=“a”
var obj=$(“#a”)的元素;
html('image');
var obj=$(“#a”);
html('image');
@QuestionOverflow肯定会在一分钟内更新答案。。我明白了,它在.html()
函数中说,这个
指的是集合中的当前元素。谢谢@问题是的,肯定会在一分钟内更新答案。。我明白了,它在.html()
函数中说,这个
指的是集合中的当前元素。谢谢因为这里被实例化为nothing。因为这里被实例化为nothing。
$('#a').html('<img src="' + $(this).attr('href') + '">image');
$('#a').html(function () {
return '<img src="' + $(this).attr('href') + '">image'
});
$('#a').html('<img src="' + $(this).attr('href') + '">image');
$('#a').html(function () {
return '<img src="' + $(this).attr('href') + '">image'
});
var obj= $("#a");
obj.html('<img src="' + obj.attr('href') + '">image');