我写过这样的代码。 <img id='test_img' src='../../..' />
如何使用jQuery获取图像ID?
我想对像图像加载这一形象的ID,
$(img).load(function() {
// Here I want to get image id i.e. test_img
});
你能帮帮我吗?
谢谢。
我写过这样的代码。 <img id='test_img' src='../../..' />
如何使用jQuery获取图像ID?
我想对像图像加载这一形象的ID,
$(img).load(function() {
// Here I want to get image id i.e. test_img
});
你能帮帮我吗?
谢谢。
$(img).load(function() {
var id = $(this).attr("id");
//etc
});
祝你好运!
编辑:
//suggested by the others (most efficient)
var id = this.id;
//or if you want to keep using the object
var $img = $(this);
var id = $img.attr("id")
感谢所有。我想你们都是对的。我尝试了所有的答案,所有的工作。感谢大家。 – gautamlakum 2010-10-08 13:31:53
$(img).load(function() {
alert($(this).attr('id'));
});
不要使用$(this).attr('id')
,它采取的长,效率低的路线。只需要this.id
是必要的,它避免了使用jQuery重新包装元素并执行attr()函数(它无论如何映射到属性!)。
$(img).load(function() {
alert(this.id);
});
$(function() {
$('img#test_img').bind('load', function() {
console.log(this.id); //console.log($(this).attr('id'));
});
});
this.id您的负载函数中。 – 2010-10-08 13:22:51
考虑@Andy E的建议。创建一个jQuery对象并调用一个方法通过直接引用属性名称来提取可用的属性值是没有意义的。 – user113716 2010-10-08 13:43:21