简体   繁体   English

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

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

<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 事件)在单击该列时拨打该电话号码?

You can simply add an onclick handler to your <tr> tag, then call window.open("tel:+1800229933");您可以简单地向<tr>标签添加一个onclick处理程序,然后调用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.使用 jQuery 查找单元格内容,替换“Phone:”部分并将其设为链接。 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: :问题标题的确定答案,因此,不使用<a>标签是制作一个辅助函数来模拟<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: .目标_self使我们看起来像呼叫模拟,就像单击<a href="tel:

Now the simulateCall helper function can be used anywhere.现在, simulateCall辅助函数可以在任何地方使用。 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');

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

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