是否可以根据屏幕大小有两个 href 链接(phone 数字和网站链接)? html/css

Is it possible to have two href links (phone number and website links) based on screen size? html/css

<a href="contact.html" href="tel:5555555555">Contact</a>

我正在尝试创建一个联系按钮,如果用户不在移动屏幕上,它将把用户带到网站 link。但是,如果用户在小于 1280 像素的屏幕上,即。 @media screen and (max-width: 1280px) {...然后我想要联系按钮拨打 link 中的号码。

我那里的任何一个 href 都可以正常工作,但我正在尝试根据屏幕尺寸获取两者。有没有办法使这成为可能?

编辑:

.button {
    -moz-appearance: none;
    -webkit-appearance: none;
    -ms-appearance: none;
    appearance: none;
    -moz-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    -webkit-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    -ms-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    background-image: -moz-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.15)), url("images/bg01.png");
    background-image: -webkit-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.15)), url("images/bg01.png");
    background-image: -ms-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.15)), url("images/bg01.png");
    background-image: linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.15)), url("images/bg01.png");
    background-color: darkgreen;
    border-radius: 5px;
    border: 0;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    padding: 0 1.5em;
    line-height: 2.75em;
    min-width: 9em;
    text-align: center;
    text-decoration: none;
    font-weight: 600;
    letter-spacing: -0.025em;
}

.button:hover {
        background-color: green;
        color: #fff !important;
    }
.button:active {
        background-color: green;
        color: #fff;
    }

#banner header .button {
            vertical-align: middle;
            margin-left: 1em;

        }

这可以通过 CSS 媒体查询 (https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries)

完成

所以基本上,您将有两个 link:

.visible-on-mobile {
  display: none;
}

.visible-on-desktop {
  display: inline;
}

@media only screen and (max-width: 640px) {
  .visible-on-mobile {
    display: inline;
  }
  .visible-on-desktop {
    display: none;
  }
}
<div class="visible-on-mobile">
    <a href="tel:0000000000" class="button">Phone</a>
</div>

<div class="visible-on-desktop">
    <a href="mailto:hi@example.com" class="button">Email</a>
</div>

当设备宽度小于或等于 640px 时,这将显示 Phone link 否则,将显示电子邮件 link。

希望对您有所帮助!