2 回答
TA贡献1828条经验 获得超13个赞
我认为您已经在单击时替换了所有 .phone,因为您使用了全局选择器。尝试将事件绑定到每个链接,或者您可以在 .phone-link 选择器上使用实时侦听器。
jQuery(function($) {
var formatPhone = function(tel) {
// format should be here
return tel;
}
// replace in a single loop
// without wrapping
$('.phone').each(function() {
var $this = $(this);
var tel = $this.text();
var $link = $('<a class="phone-link" href="tel:'+tel+'">Click to Call</a>')
.on('click', function(ev) {
ev.preventDefault();
$(this).text(formatPhone(tel));
});
$this.empty().append($link);
});
});
带有现场听众的示例
$(document).on('click', '.phone-link', function(ev) {
ev.preventDefault();
var $this = $(this);
$this.text(formatPhone( $this.attr('href') ));
});
TA贡献1866条经验 获得超5个赞
我对“点击通话”和“实际电话号码”之间的预期切换感到有些困惑。请让我知道它是否符合目的或需要更多更改
jQuery(document).ready(function($) {
$(".phone").on("click",function(event){
if(!$(event.target).hasClass("phone-link") ){
var phoneLink = $(this).text();
$(this).wrapInner('<a class="phone-link" href=""></a>');
$(this).find("a.phone-link").attr('href','tel:' + phoneLink).text('Click to Call');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="entry-details">
<p class="phone">+18885551212,,,111</p>
<p class="address">111 Any Street, Anytown, NY 10010, USA</p>
<p class="time">11:00 to 08:00</p>
</div>
<div class="entry-details">
<p class="phone">+18885551212,,,222</p>
<p class="address">222 Any Street, Anytown, NY 10010, USA</p>
<p class="time">11:00 to 08:00</p>
</div>
<div class="entry-details">
<p class="phone">+18885551212,,,333</p>
<p class="address">333 Any Street, Anytown, NY 10010, USA</p>
<p class="time">11:00 to 08:00</p>
</div>
<div class="entry-details">
<p class="phone">+16665551200</p>
<p class="address">444 Any Street, Anytown, NY 10010, USA</p>
<p class="time">11:00 to 08:00</p>
</div>
添加回答
举报