简体   繁体   中英

how to call a phone number through javascript without using <a> tag?

I designed a mobile web page. There I have a phone number field, which on clicking should call that particular number. I can't use:

<a href="tel:+1800229933"></a>

Because I have added the phone number field using table tag as follows:

<table>
  <tr>
    <td>Phone: 900 300 400</td>
  </tr>
</table>

Are there any other methods(like OnClick event) to call that phone number on clicking that column?

You can simply add an onclick handler to your <tr> tag, then call window.open("tel:+1800229933"); .

Like so:

<table>
  <tr onclick="window.open('tel:900300400');">
    <td>Phone: 900 300 400</td>
  </tr>
</table>

Change this:

<table>
  <tr>
    <td>Phone: 900 300 400</td>
  </tr>
</table>

to:

<table>
  <tr>
    <td>
      <a href="tel:+900300400">Phone: 900 300 400</a>
    </td>
  </tr>
</table>

Find the cell content with jQuery, replace the "Phone:" part and make it a link. The selection of the cell with a class is one way of doing it. Another would be to select in the real table the cell with a code similar to "the second cell in each row of the table". Here a working example:

<html>
  <head>
    <title>Test</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
      $(function() {
        $('.phonecell').click(function() {
          var PhoneNumber = $(this).text();
          PhoneNumber = PhoneNumber.replace('Phone:', '');
          window.location.href = 'tel://' + PhoneNumber;
        });
      });
    </script>
  </head>
  <body>
    <table>
      <tr>
        <td class="phonecall">Phone: 900 300 400</td>
      </tr>
    </table>
  </body>
</html>

The certain answer for the title of the question, hence, without using <a> tag is to make a helper function to simulate the action of <a href="tel: :

const simulateCall = phoneNumber => window.open(`tel:${phoneNumber}`, '_self');

The target _self causes we have an exact look like call simulation just like clicking <a href="tel: .

Now the simulateCall helper function can be used anywhere. like my case:

const callHandler = phoneNumber => () => {
  // I want to do something here then make a call
  simulateCall(phoneNumber);
};

~~~

<SomeComponent onClick={callHandler} ...

试试这个可能你会发现它很有帮助https://www.theharnishes.com/phone.html

Shorted answer

this will keep you in the same window

window.location = "tel:495898694"

while this will open another tab and then call the number

window.open('tel:900300400');

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