简体   繁体   中英

javascript/jquery simulate anchor “tel” click

I have found lots of messages about simulating an anchor click with 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.

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. 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

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

Check this 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. 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.

DEMO

I know the questions asks not to use the location function, but please try this out:

HTML:

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

jQuery: (could easily be written with only javascript too if you wanted to)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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