简体   繁体   English

javascript / jquery模拟锚点“ tel”点击

[英]javascript/jquery simulate anchor “tel” click

I have found lots of messages about simulating an anchor click with javascript. 我发现了许多有关使用javascript模拟锚定点击的消息。 Most using the "location" function, which doesn't work for me because I do not want to redirect anywhere. 大多数使用“位置”功能,该功能对我不起作用,因为我不想重定向到任何地方。 My anchor has href="tel:..." to invoke the iphone's phone function. 我的主播具有href =“ tel:...”来调用iphone的电话功能。

I can't have an anchor for clicking, so I wanted to have javascript/jquery simulate that click when a user presses a button, for example. 我没有点击的锚点,因此,例如,我想让javascript / jquery模拟用户按下按钮时的点击。 How could I do this (if, indeed, it is even possible)? 我该怎么做(如果确实有可能的话)?

Thanks 谢谢

You can trigger the click event for the anchor on on the click of the button 您可以在单击按钮时触发锚点的click事件

$('#button1').on('click', function() {
    $('#anchor1').click();  // triggering the click event on click of the button
});

Check this FIDDLE 检查这个FIDDLE

$(function() {

    $('a').on('click' , function() {

       alert('Fired using button !!')
    });

    $('#button1').on('click' , function() {

       $('a').click();

        alert('Button Clicked !!')
    });

});​

尝试这个:

$(your_button).trigger('click')

您可以触发点击事件...

$("a.phonenumber").click();

Here is a simple example of how to accomplish this - 这是有关如何完成此操作的简单示例-

$(function(){
    $("#b1").on('click',function(){
      alert("pressed b1!");
    });        

     $("#b2").on('click',function(){
      $("#b1").trigger('click');
      alert("pressed b2!");
    });        
});​

Clicking on the first b1 button will trigger an alert. 单击第一个b1按钮将触发警报。 When clicking the second b2 button, first we "trigger" a click event on the first button (which will execute it's alert), and then alert the second value. 当单击第二个b2按钮时,首先我们“触发”第一个按钮上的click事件(它将执行警报),然后警报第二个值。

DEMO DEMO

I know the questions asks not to use the location function, but please try this out: 我知道问题要求不要使用定位功能,但请尝试以下操作:

HTML: HTML:

<button class="click-to-call" data-phone="2125551234">call</button>

jQuery: (could easily be written with only javascript too if you wanted to) jQuery :(如果愿意,也可以很容易地仅用javascript编写)

$('.click-to-call').on('click',function(){
    window.location.assign('tel:'+ $(this).data('phone'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM