我想记录在android/iOS设备上显示的网页上显示的用户触摸并拨打号码的所有事件。有可能吗?
发布于 2013-01-24 18:34:59
至少有三种方法可以解决这个问题:
单击1.在()上检查href
您可以监听链接上的click()事件,并检测其href是否以tel:开头
$( 'a' ).click( function () {
var href = $( this ).attr( 'href' );
var tel = /^tel:/i.test( href );
if ( tel ) {
alert( 'A telephone link was clicked: ' + href );
//if you don't want the default action (dial ) to happen, return false, otherwise don't return anything
}
});JSFiddle直播:http://jsfiddle.net/c7CRG/2/
2.使用类标志
此外,如果您可以控制HTML,并且可以在包含电话号码的所有<a>链接中放入一个类名,则不需要在上面的算法中进行解析:
$( 'a.telephone' ).click( function () {
alert( 'A telephone link was clicked: ' + $( this ).attr( 'href' ) );
//if you don't want the default action (dial ) to happen, return false, otherwise don't return anything
});3.使用高级JQuery语法
您不必使用regexp来解析href属性。JQuery为此提供了an interesting syntax:
$( 'a[href^="tel:"]' ).click( function () {
alert( 'A telephone link was clicked: ' + $( this ).attr('href' ) );
});下面是它的JSFiddle:http://jsfiddle.net/c7CRG/7/
https://stackoverflow.com/questions/14499025
复制相似问题