Auto Click With Type or Value Tag

后端 未结 1 1138
既然无缘
既然无缘 2021-01-15 06:37

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 :-



        
相关标签:
1条回答
  • 2021-01-15 07:08

    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.

    0 讨论(0)
提交回复
热议问题