Auto Click With Type or Value Tag

六月ゝ 毕业季﹏ 提交于 2019-12-30 10:59:09

问题


Dear friends i have site for which i would like to create javascript bookmarklet for autoclick, but the site does not use ID tag. here is the code of site :-

<input type="submit" name="Submit" value="Find Product" 
onclick="return incrementClicksTest('2','true')" class="buttonSubmit">

I used this JavaScript bookmarklet

javascript:document.getElementById('any_id_here').click()

workes fine for id, how do i go about making bookmarklet using name,value and type tag

thanks dvance!!


回答1:


Use the following:

document.getElementsByTagName("input")[0].click();

Sample Code: http://jsfiddle.net/dcRsc/

Now, that will work if your button is the first input in your page.

Use this if you have numerous elements in your page:

var elems =document.getElementsByTagName("input");

for(var i=0;i<elems.length;i++)
{
    if(elems[i].type=="submit" && elems[i].name =="Submit")
    {
        elems[i].click();        
        break;
    }
}

Sample Code: http://jsfiddle.net/dcRsc/1/

That will trigger the click event of your submit button, with Submit name.

Furthermore (and since your button already has a css class) you could use the getElementsByClassName() method:

var elems =document.getElementsByClassName("buttonSubmit");

for(var i=0;i<elems.length;i++)
{
    if(elems[i].name =="Submit")
    {
        elems[i].click();        
        break;
    }
}

Sample Code: http://jsfiddle.net/dcRsc/2/

That will get all elements with the buttonSubmit class applied.

Or

document.getElementsByClassName("buttonSubmit")[0].click();

If your button is the only element in the page with that class on it, hence avoiding the for loop altogether.



来源:https://stackoverflow.com/questions/15580888/auto-click-with-type-or-value-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!