admin管理员组文章数量:1357583
Sorry if that question is confusing, I'm self-learning javascript. I'm dynamically generating image thumbnails and I want to be able to enlarge the image when the user clicks on the thumbnails. My code to create the image tag and assign the onclick function looks like...
var imageTag = "<img onclick=\"enlargeImage()\" class=\"thumb\" src=\"" + photoURL + "\" />";
document.getElementById("image-thumbnail").innerHTML = imageTag;
With that code above, my thumbnail gets displayed and when the user clicks on it, the enlargeImage()
function is called. My question is, how can I access the image object that was clicked inside the enlargeImage()
function? I tried accessing the this
object inside the function hoping it pointed to the image that was clicked, but this
referred to the entire page, not the image that was clicked. I'd like to be able access the src
attribute, as well as, change the style attributes of the image thumbnail. I should also note that eventually there will be multiple images which is why I need this dynamic behavior.
Thanks so much in advance for your help!
Sorry if that question is confusing, I'm self-learning javascript. I'm dynamically generating image thumbnails and I want to be able to enlarge the image when the user clicks on the thumbnails. My code to create the image tag and assign the onclick function looks like...
var imageTag = "<img onclick=\"enlargeImage()\" class=\"thumb\" src=\"" + photoURL + "\" />";
document.getElementById("image-thumbnail").innerHTML = imageTag;
With that code above, my thumbnail gets displayed and when the user clicks on it, the enlargeImage()
function is called. My question is, how can I access the image object that was clicked inside the enlargeImage()
function? I tried accessing the this
object inside the function hoping it pointed to the image that was clicked, but this
referred to the entire page, not the image that was clicked. I'd like to be able access the src
attribute, as well as, change the style attributes of the image thumbnail. I should also note that eventually there will be multiple images which is why I need this dynamic behavior.
Thanks so much in advance for your help!
Share Improve this question asked Dec 1, 2009 at 19:32 BeachRunnerFredBeachRunnerFred 18.6k35 gold badges143 silver badges241 bronze badges3 Answers
Reset to default 8There are a few ways of doing this. The easiest is to simply add a a parameter to the enlargeImage()
function and pass it a this
reference when you call it.
var imageTag = "<img onclick=\"enlargeImage(this)\" class=\"thumb\" src=\"" + photoURL + "\" />";
Then in the function...
function enlargeImage(img) {
alert(img.src);
}
I suggest checking out jQuery, since it has a lot of wrappers and handlers for dealing with this stuff, and has a lot of momentum recently.
To be more specific to your question, you can just pass "this" to your function:
var imageTag = "<img onclick=\"enlargeImage(this)\" class=\"thumb\" src=\"" + photoURL + "\" />";
and then in your function you'll have access to the src tag:
function enlargeImage(img){
alert(img.src);
}
function enlargeImage(img)
{
var src = img.src;
}
版权声明:本文标题:javascript - How do I pass the image object into the onclick function being assigned to it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744077799a2587069.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论