admin管理员组文章数量:1336684
This is my second question about clicking buttons in JavaScript but this one I'm really stuk on: I'm trying to click an image button with javascript however there's not much to go on as the source only gives this information about the image button
<div id="claimBtnDv" style="bottom:-30px; position:absolute; right:0; margin-top:0;">
<input type="image" class="btnClaim" src="websitebuttonimage" onclick="alert('buttonclicked');">
</div>
I tried to do document.getElementById('claimBtnDv').click()
but no success , are there other methods to use?
This is my second question about clicking buttons in JavaScript but this one I'm really stuk on: I'm trying to click an image button with javascript however there's not much to go on as the source only gives this information about the image button
<div id="claimBtnDv" style="bottom:-30px; position:absolute; right:0; margin-top:0;">
<input type="image" class="btnClaim" src="websitebuttonimage" onclick="alert('buttonclicked');">
</div>
I tried to do document.getElementById('claimBtnDv').click()
but no success , are there other methods to use?
- 1 it works for me - jsfiddle/wMRbn – Zoltan Toth Commented Jun 21, 2012 at 21:59
- You get the message 'button clicked' when you use document.getElementById('claimBtnDv').click() ? – Nubcake Commented Jun 21, 2012 at 22:00
3 Answers
Reset to default 3If you can't change the HTML and you need to click the image using javascript, you can go into the child nodes to find the image. This is pretty easy since a class name is provided.
var nodes = document.getElementById('claimBtnDv').childNodes;
for ( i = 0; i < nodes.length; i++ ) {
if ( nodes[i].className === "btnClaim" ) {
console.log("clicking!");
nodes[i].click();
break;
}
}
Your code almost works - just change your class="btnClaim"
to id="btnClaim"
- and the getElementById
will work - http://jsfiddle/wMRbn/1/
document.getElementById('btnClaim').onclick = function () {
alert ( "click" );
}
UPDATE
To get an element without an id
you can do this - get the parent element by id
and attach an event listener to its' input
child - http://jsfiddle/wMRbn/3/
var img = document.getElementById("claimBtnDv").getElementsByTagName("input")[0];
img.onclick = function () {
alert ( "click" );
}
it looks like it works in that case. Are you trying to assign it different click events or something?
jquery would allow you to do something like:
$("input.btnClaim").click(function(){
//in here can go a lot more functions, etc.
});
javascript would do (after you give it an id of 'btnClaim'):
var X = document.getElementById("btnClaim");
x.onclick = "JAVASCRIPT GOES HERE";
//from what the wc3 doc looks like, you need to have it in quotes,
// but i may be mistaken.
Maybe that helps ?
本文标签: JavaScript Click Image ButtonStack Overflow
版权声明:本文标题:JavaScript Click Image Button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223479a2435600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论