使用javascript创建一个包含多个条目的VCard文件

问题描述 投票:0回答:1

我使用了这个github gist中的代码,它允许您将单个内容保存到VCF文件中。

<!doctype html>
<html>
    <head>
       <script type="text/javascript" src="vcard2.js"></script>
    </head>
    <script type="text/javascript">
        // From JSON
        var johnSmith = {
            "version": "4.0",
            "n": "SMITH;John;;",
            "fn": "John Smith",
            "nickname":"js",
            "title": "Missing man too",
            "tel": [
                {"value": "555-555-555", "type": "cell"}
            ],
            "email": [
                { "value": "[email protected]", "type": "work" },
                { "value": "[email protected]", "type": "home" }
            ]
        }
        var link = vCard.export(johnSmith, "John Smith", false) // use parameter true to force download
        document.body.appendChild(link)
        </script>
    </body>
</html>

这适用于单个内容作为文件请参阅JSfiddle。我的问题是如何将多个内容添加到同一个VCF文件中。

javascript json vcard
1个回答
0
投票

在你提供的小提琴中,只需编辑dump()函数即可接受数组。然后迭代数组并执行完全相同的操作,将输出(用\n拆分)组合成一个变量。

    dump: function(cards) {
        var cardsLength = cards.length;
        var cardsStr = "";

        for (var cardsIndex = 0; cardsIndex < cardsLength; cardsIndex++) {

            var card = cards[cardsIndex];
            //...existing code... just remove the return
             cardsStr+=str+"\n";
        }
        return cardsStr;
    }

现在调用vCard.export()并传递一个对象数组而不是对象。

试试吧:JSFiddle

© www.soinside.com 2019 - 2024. All rights reserved.