怎么用JS调用打电话

JavaScript015

怎么用JS调用打电话,第1张

用JS调用打电话使用这句话就可以:

<a id="call">拨打电话</a>

document.getElementById('call').setAttribute('href','tel:123')

举个栗子,最常见的情况是用ajax动态为标签赋电话值,然后拨打电话,比如说

//a标签是你想点击拨打电话的地方

$.ajax({

type: "POST",

url: WebUrl ,

data: {},

success: function (data) {

//调用成功的时候,将返回的电话号动态赋值到a标签中

document.getElementById('call').setAttribute('href','tel:'+shopTel)

//shopTel是后台返给前端的电话号,这样电话号码就被动态赋值到a标签里啦

},

})

在移动端页面开发中,偶尔会需要唤起用户手机的打电话功能,拨打客服电话,此时我们可以按照以下操作实现打电话功能:

1)index.html在<head></head>中加入这一段:

<meta name="format-detection" content="telephone=yes"/>

2)js中设置点击事件:

window.location.href = 'tel:4000-000-000'

或者直接使用:

<a href="tel:4000-000-000">拨打电话</a>

若涉及到发送短信可以设置:

// 添加内容

window.location.href = 'sms:10086?body=短信内容'

// 不添加内容

window.location.href = 'sms:10086'

或者:

<a href="sms:10086">发送短信</a>

<a href="sms:10086?body=短信内容"></a>

文章参考: https://blog.csdn.net/Boale_H/article/details/108582436