admin管理员组

文章数量:1417460

This was my attempt. The best that I can e up with is getting the link to open up inside the chrome extension. I'd like it to open in a new tab.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link href="css/style.css" rel="stylesheet"/>
        <script type="text/javascript">
            var items = [];
            var background;

            function init() {
                background = chrome.extension.getBackgroundPage();
                items = background.items;

                createItemTable();
            }

            function createItemTable() {
                var content = document.getElementById("content");
                var list = document.createElement("div");
                list.setAttribute("class", "list");
                content.appendChild(list);

                for (x in items) {
                    var item = items[x];

                    var link = document.createElement("a"); // create the link
                    link.setAttribute('href', "'" + item["link"] + "'"); // set link path
                    link.setAttribute("onclick", "openTab('" + item["link"] + "');");

                    var titleNode = document.createElement("div");
                    titleNode.setAttribute("class", "title");
                    titleNode.appendChild(document.createTextNode(item["title"]));

                    link.appendChild(titleNode);
                    result.appendChild(link);

                    list.appendChild(result);
                }

            }

        </script>
    </head> 
    <body onload="init();" onunload="background.updateBadge();"> 
        <div id="content"></div>
    </body>
</html>

This was my attempt. The best that I can e up with is getting the link to open up inside the chrome extension. I'd like it to open in a new tab.

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link href="css/style.css" rel="stylesheet"/>
        <script type="text/javascript">
            var items = [];
            var background;

            function init() {
                background = chrome.extension.getBackgroundPage();
                items = background.items;

                createItemTable();
            }

            function createItemTable() {
                var content = document.getElementById("content");
                var list = document.createElement("div");
                list.setAttribute("class", "list");
                content.appendChild(list);

                for (x in items) {
                    var item = items[x];

                    var link = document.createElement("a"); // create the link
                    link.setAttribute('href', "'" + item["link"] + "'"); // set link path
                    link.setAttribute("onclick", "openTab('" + item["link"] + "');");

                    var titleNode = document.createElement("div");
                    titleNode.setAttribute("class", "title");
                    titleNode.appendChild(document.createTextNode(item["title"]));

                    link.appendChild(titleNode);
                    result.appendChild(link);

                    list.appendChild(result);
                }

            }

        </script>
    </head> 
    <body onload="init();" onunload="background.updateBadge();"> 
        <div id="content"></div>
    </body>
</html>
Share Improve this question asked Jul 6, 2011 at 17:10 JonathanJonathan 3,0249 gold badges46 silver badges75 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

try

link.setAttribute('target','_blank');

本文标签: javascriptOpen link in new tabStack Overflow