phonegap 获取设备原生的通讯录信息

2018-03-23  本文已影响0人  LIsPeri

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Contact Example</title>
<style type="text/css">
.contact{padding:5px;border-bottom:1px solid gray;}
</style>
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var options = new ContactFindOptions();
var fields = ["displayName", "phoneNumbers"];
options.multiple = true;
navigator.contacts.find(fields, onSuccess, onError,options);
}
var list = $("#contacts");
function onSuccess(contacts) {
for (var i=0; i<contacts.length; i++) {
$("<p class='contact'/>").text(contacts[i].displayName)
.data("name", contacts[i].displayName)
.data("number", contacts[i].phoneNumbers[0].value)
.click(function(){
$("#name").text($(this).data("name"));
$("#number").text($(this).data("number"));
$("#detail").show();
$("#contacts").hide();
}).appendTo(list);
}
}
function onError(contactError) {
alert('onError!');
}
$("#back").click(function(){
$("#detail").hide();
$("#contacts").show();
});
});
</script>
</head>
<body>
<h1>通讯录</h1>
<div id="contacts"></div>
<div id="detail" style="display:none">
姓名:<span id="name"></span>

手机号:<span id="number"></span>

<div style="text-align:center;margin-top:10px"><button id="back" style="width:90px;height:60px;">返回</button></div>
</div>
</body>
</html>

上一篇下一篇

猜你喜欢

热点阅读