简体   繁体   English

如何在innerHTML中添加呼叫电话号码链接

[英]How to add Call a phone number link in innerHTML

I am trying to add a call specific phone number using <a href> tag inside innerHTML . 我正在尝试使用innerHTML内部的<a href>标签添加特定于呼叫的电话号码。 I've tried with double and single quotes. 我试过用双引号和单引号。

1st Case is not firing at all. 第一种情况完全没有触发。

2nd Case it does appear but when clicking on the phone number rather than dialing it closes. 第二种情况确实会出现,但是单击电话号码而不是拨打电话时,它将关闭。

 var popDiv = document.createElement('span');
           popDiv.setAttribute('class', 'popDiv');
           popDiv.innerHTML ="It seems you are looking for: " + "<span style='color:#FF0000'>" + getTitle + "</span>" + "<br />" + "Why don't you call me?" + "<a href='tel:01234567890'>01234 567 890</a>";

Please find link to the JSFiddle 请找到指向JSFiddle的链接

Does anyone know what a possible solution would be? 有谁知道可能的解决方案?

You need to check the mouseup event's target and if it's h1 , then only remove the popDiv . 您需要检查mouseup事件的target ,如果它是h1 ,则仅删除popDiv This should work : 这应该工作:

$("#"+parentContainerId).on('mouseup', function(e){
    if(event.target.tagName.toLowerCase() === 'h1') {
        $('span.selectedText').contents().unwrap();
        $(this).find('span.popDiv').remove();
    }
});

Updated jsFiddle 更新了jsFiddle

Just try with that : 只需尝试:

 var popDiv = document.createElement('span');
           popDiv.setAttribute('class', 'popDiv');
           popDiv.innerHTML ="It seems you are looking for: " + "<span style='color:#FF0000'>" + getTitle + "</span>" + "<br />" + "Why don't you call me?" + "<a href=\"tel:01234567890\">01234 567 890</a>";

and be sure you have not any meta like that : 并确保您没有像这样的元数据:

<meta name = "format-detection" content = "telephone=no">

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

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