2012-09-02 5 views
18

I have a bunch of <a href=".html"> links and want to make them all open in new windows.¿Hay alguna manera de abrir todos <a href> links on a page in new windows?

I know I can do a search and replace all to add target="_blank" to all my <a href="..."> links.

However, is there a quick way, such as setting a CSS style, or adding some JavaScript code, to accomplish the same thing?

+2

Puede buscar y reemplazar su proyecto. – Krycke

+1

la refactorización siempre es mejor que las soluciones temporales. Te ahorras mucho trabajo a largo plazo. Tan solo haga una búsqueda y reemplace en lugar de introducir alguna solución de javascript que también ralentice su página. – Christoph

+1

Dije que sé que puedo hacer una búsqueda y reemplazar -_- –

Respuesta

24

If you have a page consisting of only links, consider <base target="_blank">? Esto abre cada enlace en una nueva ventana (pero también incluye los objetivos de las formas, a no ser anulado con <form target="_self">.

Como otros han demostrado, sin modificar el código fuente HTML, puede utilizar Javascript para iterate through all <a> tags and add the target attribute o agregar una event listener that sets the target attribute dynamically.

+1

Este es el mejor. BASE TARGET es lo que necesitaba. ¡Gracias! aceptará pronto –

7

CSS: No.
JavaScript: Delegate a click event, which adds a target="_blank" attribute on click of a link.

document.body.addEventListener(function(e) { 
    if (e.target.nodeName.toUpperCase() === 'A' && e.target.href) { 
     e.target.target = '_blank'; 
    } 
}, true); 

Note: If the <a> element contains other elements, you might want to traverse the tree to find out whether an anchor element is clicked:

document.body.addEventListener(function(e) { 
    var target = e.target; 
    do { 
     if (target.nodeName.toUpperCase() === 'A' && target.href) { 
      target.target = '_blank'; 
      break; 
     } 
    } while (target = target.parentElement); 
}, true); 

Or, if you're a jQuery-lover:

$('body').on('click', 'a', function(e) { 
    e.target.target = '_blank'; 
}); 
24

If you have jQuery it's simple

$("a").attr("target", "_blank"); 

Or regular Javascript

var links = document.links; 
for (var i = 0; i < links.length; i++) { 
    links[i].target = "_blank"; 
} 

As per @Lekensteyn's suggestion, without Javascript (added for Completeness)

<base target="_blank">. 
+1

En lugar de 'getElementsByTagName (" a ")' debes usar 'document.links'. Esto tiene como ventaja que solo incluya enlaces (es decir, elementos ' ') y no algo como' '. – Lekensteyn

+1

Esto no funcionará para los enlaces que se agregan después de llamar al script. –

0

sí, u puede agregar atributo con JS para todos los enlaces en el documento 'objetivo' HTML denominado con valor '_blank';)

también puede abrir DT de href de todos los enlaces de URL para javascript: OpenInWindow (URL) usando esto, y la función de escritura en JS que abre una nueva ventana y establece su ubicación en url;) Google lo ayudará con ambos.

Cuestiones relacionadas