简体   繁体   中英

Click multiple links at once

I have this bit of javascript:

 function refreshGroup(){
  var obj1 = document.getElementById("refresh0:link0");
  var obj2 = document.getElementById("refresh1:link1");
  obj1.click();
  obj2.click();
 };

and will ultimately be adding several more objects to the list. Each object is a link on the page. I want to create a method that refreshes all of the links for a particular group of links. The links are tied to a backing bean method which updates the data associated with the link. This does what I want it to except that it waits for each link to finish running before clicking on the next link. Is there any way to make it click on all of them at once?

Edit:

Here's the bit of code that I'm trying to simulate clicking on (JSF):

<h:form id="refresh#{loop.index}" >
  <h:commandLink class="link" id="link#{loop.index}" value="#{a.status}" >
    <f:ajax listener="#{a.getCheckAppMonitor}" 
            render=":errs#{loop.index} :refresh#{loop.index} :errs_#{i+1}"
        onevent="refreshMoreLessLinks" />
  </h:commandLink>&nbsp;<img id="img_#{loop.index}"
                             class="hidden"
                             src="resources/images/ajax-loader.gif"/>
</h:form>

JSF generates this into something that looks like this:

<FORM id=refresh0 encType=application/x-www-form-urlencoded method=post 
  name=refresh0 action=/appmonitor/faces/appmonitorgreeting.xhtml><INPUT 
  value=refresh0 type=hidden name=refresh0> <A id=refresh0:link0 
  onclick="mojarra.ab(this,event,'action',0,'errs0 refresh0 errs_1',{'onevent':refreshMoreLessLinks});return false" 
  href="http://localhost:8080/appmonitor/faces/appmonitorgreeting.xhtml#">OK</A>&nbsp;<IMG 
  id=img_0 class=hidden 
  src="appmonitorgreeting_xhtml_files/ajax-loader.gif"><INPUT 
  id=javax.faces.ViewState value=7494473519347208075:5184408304577602221 
  type=hidden name=javax.faces.ViewState autocomplete="off"> </FORM>

Don't try to spoof click events.

Call the function behind the click event instead.

Treat the DOM as a view, not a model.

Is there any way to make it click on all of them at once?

As per my knowledge, its not possible.Because clicking the element adds an event in the event queue managed by browser. When you execute the statement obj1.click(); , it registers the click event attached with obj1 . When this event reaches the front of event queue, browser calls the corresponding event handler attached.
While some event handler code is executing, all the other events triggered are added in the queue and they wait for their turn to move to the front of queue.So you can click on elements one after another but the event handlers attached with them will execute one at a time.
Reason: Because JavaScript uses single thread of execution

If one event handler is doing a large calculation, all the events triggered will have to wait for that calculation to complete.
try this fiddle : here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM