5条回答
  • 2020-12-25 11:35

    yep, u can add attribute with JS to all links in HTML document named 'target' with value '_blank' ;)

    You could also open replace href's of all links from url to javascript:openInWindow(url) using this, and writing function in JS that opens new window and set's it's location to url ;) Google will help you with both.

    0 讨论(0)
  • 2020-12-25 11:48

    If you have a page consisting of only links, consider <base target="_blank">. This opens every link in a new window (but also includes the targets of forms, unless overridden with <form target="_self">.

    As others have shown, without modifying the HTML source, you can use Javascript to iterate through all <a> tags and add the target attribute or add an event listener that sets the target attribute dynamically.

    0 讨论(0)
  • 2020-12-25 11:48

    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';
    });
    
    0 讨论(0)
  • 2020-12-25 11:49

    Just add a html base element to the page using Javascript:

    var e = document.createElement("base");
    e.target = "_blank";
    document.head.appendChild(e); 
    
    0 讨论(0)
  • 2020-12-25 11:50

    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">.
    
    0 讨论(0)
提交回复
热议问题