JQuery - opening all links on a page

I'm trying to learn jQuery to make up for my anemic JavaScript skills.

As a test project, I have a page full of links, and I want buttonthis page to open all links in new tabs. All links have attributes target="_blank".

I use this

  $('button').click(function() {
    $('a').click();
  );}

I tested the syntax selectorby changing the csslinks, so I'm sure everything is in order. What do I need to change to open the link?

+4
source share
1 answer

you cannot manipulate tabs through javascript (you can ask the link to open in a new window, you just can't say that it opens on the tab). what you can try if you want to try is something like this:

$('button').click(function() {
  $('a').each(function() {
     window.open($(this).attr('href') );
  });
});

, <button> <a> href window.open. , , , :)

, <button>, onclick() <a>.

: , OP:

$('a').click(function() {
// assign an event to a.onclick
  window.open($(this).attr('href') );
});

$('button').click(function() {
// when we press <button>, trigger a.onclick
  $('a').click();
});

onclick(), , . ( )

+11

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


All Articles