Html 如何在不使用 <a> 标签的情况下通过 javascript 拨打电话号码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17897213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:34:56  来源:igfitidea点击:

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

javascripthtml

提问by msg

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 标记添加了电话号码字段,如下所示:

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

是否有任何其他方法(如 OnClick 事件)在单击该列时拨打该电话号码?

采纳答案by Ehsan

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>

回答by idmadj

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

您可以简单地onclick向您的<tr>标签添加一个处理程序,然后调用window.open("tel:+1800229933");.

Like so:

像这样:

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

回答by user1525604

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:

使用 jQuery 查找单元格内容,替换“Phone:”部分并将其设为链接。选择带有类的单元格是一种方法。另一种方法是在实际表格中选择具有类似于“表格每一行中的第二个单元格”的代码的单元格。这是一个工作示例:

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

回答by AmerllicA

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

问题标题的确定答案,因此,不使用<a>标签是制作一个辅助函数来模拟以下动作<a href="tel:

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

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

目标_self使我们有一个确切的样子电话模拟就像点击<a href="tel:

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

现在simulateCall辅助函数可以在任何地方使用。就像我的情况:

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

~~~

<SomeComponent onClick={callHandler} ...