【问题标题】:Adding Href Via JQuery通过 JQuery 添加 Href
【发布时间】:2019-10-02 20:50:24
【问题描述】:

我正在尝试使用 jquery 使电话号码可点击。有现有的 css 代码和 jquery 来创建 html,但是我似乎无法添加 href。

尝试将 span 类更改为包含 href,添加后电话号码不显示

jQuery(document).ready(function() {
  $phone_number2 = "International 00-1-250-586-9928";
  jQuery("span#et-info-phone").after("<span class=info-phone-2>" + $phone_number2 + "</span>");
  jQuery(".info-phone-2").attr("href", "tel:00-1-250-586-9928");
});
span.info-phone-2 {
  position: relative;
}

.info-phone-2:before {
  position: relative;
  top: 2px;
  margin-right: 2px;
  content: "\e090";
}

.info-phone-2 {
  margin-right: 13px;
}

.info-phone-2:before {
  font-family: "ETmodules" !important;
  font-weight: normal;
  font-style: normal;
  font-variant: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  line-height: 1;
  text-transform: none;
  speak: none;
}

【问题讨论】:

  • 跨度没有 href 属性,锚点有
  • 将span 更改为a。
  • 支点(忽略跨度问题);您正在使用字符串连接将文本添加到跨度。为什么不在那里连接 href 呢?
  • @Barmar 我这样做了,现在电话号码没有出现
  • 请更新您的问题以显示您更新后的逻辑。

标签: jquery


【解决方案1】:

使用锚点而不是跨度。

jQuery(document).ready(function() {
  $phone_number2 = "International 00-1-250-586-9928";
  jQuery("span#et-info-phone").after("<a class=info-phone-2>" + $phone_number2 + "</a>");
  jQuery(".info-phone-2").attr("href", "tel:00-1-250-586-9928");
});
a.info-phone-2 {
  position: relative;
}

.info-phone-2:before {
  position: relative;
  top: 2px;
  margin-right: 2px;
  content: "\e090";
}

.info-phone-2 {
  margin-right: 13px;
}

.info-phone-2:before {
  font-family: "ETmodules" !important;
  font-weight: normal;
  font-style: normal;
  font-variant: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  line-height: 1;
  text-transform: none;
  speak: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="et-info-phone">Phone: </span>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2021-12-30
    • 2020-11-16
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多