admin管理员组

文章数量:1136458

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?

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?

Share Improve this question edited Apr 12, 2020 at 19:46 AmerllicA 32.4k17 gold badges143 silver badges166 bronze badges asked Jul 27, 2013 at 11:26 msgmsg 1,5403 gold badges19 silver badges28 bronze badges 1
  • 8 What is the reason you cannot add the a tag to the table cell?? – PeeHaa Commented Jul 27, 2013 at 11:27
Add a comment  | 

7 Answers 7

Reset to default 118

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

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');

Try this may be you'll find it helpful https://www.theharnishes.com/phone.html

If you need to open the dialer of mobile phone when clicking on a button, you can try this:

            onClickBtn={() => window.open(`tel:+919876543210`)}

example:

           <ActionButton
            onClickBtn={() => window.open(`tel:+919876543210`)}
            title="Call Owner"
            icon={<LeftCircleTwoTone />}
          />

本文标签: htmlhow to call a phone number through javascript without using ltagt tagStack Overflow