Href 行为 - 需要澄清
Href behaviour - need clarity
我网站的个人资料页面上有这样的代码。
<div class="col-md-4">
<h2 class="textclass2" style="text-decoration: underline; margin-bottom: 10px"><%=playerdata.Player_Name%> AKA <%=playerdata.Player_NickName%></h2>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_FBLink%>" id="fbclick" onClick="checkUrl()" style="font-size: 12px; color:#40FF40; margin-bottom: 10px "> Facebook </a>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_InstagramLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Instagram </a>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_LinkedinProfile%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> LinkedIN </a>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_TwitterLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>
这是使用 EJS 完成的。它们基本上是具有用户社交页面的 URL 字符串的 href 标签。后端是 MongoDB
问题 - 如果我在数据库的相应字段中输入有效的 URL,我将被重定向到正确的 URL。但是,如果字符串为空,则会向 /profile/:username 发送 GET 请求。
这是 href 标签的正常行为方式吗?如果是,有什么规避的方法。基本上,如果字符串为空,我不想执行 GET 调用,而只是向用户显示一条消息,说明字符串为空。
如果没有 href,则 <a>
的行为与 <span>
相同。如果有一个空白 href
那么它将从地址栏中恰好是 URL 的任何地方继承它。所以如果没有,一个选项是根本不打印 link,比如 Twitter link
如果你想显示一些警报,其他选项是使用 href:script
<% if (playerdata.Player_TwitterLin) { %>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_TwitterLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>
<% } else { %>
<a class="btn btn-dark btn-xs" href="javascript:alert('Twitter link not available')" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>
<% } %>
我网站的个人资料页面上有这样的代码。
<div class="col-md-4">
<h2 class="textclass2" style="text-decoration: underline; margin-bottom: 10px"><%=playerdata.Player_Name%> AKA <%=playerdata.Player_NickName%></h2>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_FBLink%>" id="fbclick" onClick="checkUrl()" style="font-size: 12px; color:#40FF40; margin-bottom: 10px "> Facebook </a>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_InstagramLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Instagram </a>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_LinkedinProfile%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> LinkedIN </a>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_TwitterLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>
这是使用 EJS 完成的。它们基本上是具有用户社交页面的 URL 字符串的 href 标签。后端是 MongoDB
问题 - 如果我在数据库的相应字段中输入有效的 URL,我将被重定向到正确的 URL。但是,如果字符串为空,则会向 /profile/:username 发送 GET 请求。
这是 href 标签的正常行为方式吗?如果是,有什么规避的方法。基本上,如果字符串为空,我不想执行 GET 调用,而只是向用户显示一条消息,说明字符串为空。
如果没有 href,则 <a>
的行为与 <span>
相同。如果有一个空白 href
那么它将从地址栏中恰好是 URL 的任何地方继承它。所以如果没有,一个选项是根本不打印 link,比如 Twitter link
如果你想显示一些警报,其他选项是使用 href:script
<% if (playerdata.Player_TwitterLin) { %>
<a class="btn btn-dark btn-xs" href="<%=playerdata.Player_TwitterLink%>" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>
<% } else { %>
<a class="btn btn-dark btn-xs" href="javascript:alert('Twitter link not available')" role="button" style="font-size: 12px; color:#40FF40; margin-bottom: 10px"> Twitter </a>
<% } %>