Links to clicks though javascript

I am looking for how to click links on my page using javascript. On my page, I show every entry from my database. and the link “show” to the right click, it will expand and display the line in detail. I want to expand all the lines at once. Is it possible to click a link in Javascript like this?

+3
source share
1 answer

You can navigate all the anchors and click on them, yes:

var myLinks = document.getElementsByTagName("a");
for (var i = 0; i < myLinks.length; i++) {
  myLinks[i].click();
}

Note that this will click every link on the page. You can focus more on a specific set of links by going through the parent container that is outside of them:

var myLinks = document.getElementById("container").getElementsByTagName("a");

, id "container".

Update2:

, , id . , :

jQuery, id='reviews-show...' :

$("a[id^='reviews-show']").click(); // simple, eh?

^= , , "review-show", a, .

javascript, , :

var myLinks = document.getElementsByTagName("a");
for (var i = 0; i < myLinks.length; i++) {
  currentlink = myLinks[i];
  if (currentlink.id.substring(0,12) === "reviews-show") {
    currentlink.click();
  }
}

Update:

- jQuery , . jQuery ( ), :

$("a").click();           //clicks all links on page
$("#container a").click();//clicks all links within element having id 'container'

, , :

HTMLElement.prototype.click = function() {
  if (document.createEvent) {
    var evt = this.ownerDocument.createEvent('MouseEvents');
    evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
    this.dispatchEvent(evt);
  } else if (this.fireEvent) {
    this.fireEvent("onclick");
  }
}

: http://www.barattalo.it/2009/11/18/click-links-with-javascript/

+2

Source: https://habr.com/ru/post/1732716/


All Articles