[英]Hide li tag if the URL is empty
I am using the following to show a users facebook and twitter links. 我使用以下内容向用户显示Facebook和Twitter链接。
<ul class="list-inline">
<li style="padding-right:20px;">
<a href="https://{{channel.facebook_page}}" rel="nofollow" target="_blank" title="{{channel.channel_name}} On FaceBook"><i aria-hidden="true" class="fa fa-facebook fa-2x" style='color:#ee6f00'></i></a>
</li>
<li>
<a href="https://{{channel.twitter_page}}" rel="nofollow" target="_blank" title="{{channel.channel_name}} On Twitter"><i aria-hidden="true" class="fa fa-twitter fa-2x" style='color:#ee6f00'></i></a>
</li>
</ul>
Sometimes my site users don't have facebook or twitter accounts. 有时我的网站用户没有Facebook或Twitter帐户。 So is there a way of hiding the
li
if the href
value after the https://
is empty. 因此,如果
https://
之后的href
值为空,有没有一种隐藏li
的方法。 In other words if my {{channel.facebook_page}}
is blank hide th li
. 换句话说,如果我的
{{channel.facebook_page}}
是空白隐藏th li
。
A script would be one obvious solution, though there is one more. 脚本将是一个显而易见的解决方案,尽管还有更多解决方案。
Use the CSS attribute selector [attribute='value']
, and do something like this. 使用CSS属性选择器
[attribute='value']
,并执行类似的操作。
a[href='https://'] {
display: none;
}
And for the padding set on the li
, use a margin
on the anchor a
instead, and you can solve this with CSS alone, and get the same visual result as hiding the li
. 对于在
li
上设置的填充 ,请在锚点a
上使用margin
,您可以单独使用CSS来解决此问题,并获得与隐藏li
相同的视觉效果。
Not knowing what framework you are using for templating I will take a stab at it and guess Angular based on your tags in your profile. 不知道您要使用哪个框架进行模板制作,我将对其进行测试,然后根据您个人资料中的标签猜测Angular。
You can use Angular's with ng-if to show the element if it evaluates to be truthy 您可以将Angular与ng-if一起使用,以显示元素是否为真
<li
ng-if="channel.facebook_page"
style="padding-right:20px;">
<a href="https://{{channel.facebook_page}}" rel="nofollow" target="_blank" title="{{channel.channel_name}} On FaceBook"><i aria-hidden="true" class="fa fa-facebook fa-2x" style='color:#ee6f00'></i></a>
</li>
You could do the a[href='https://']
selector that @LGSon used in his CSS, but use it in your javascript, then get the parentElement and set it to display: none
您可以执行@LGSon在他的CSS中使用的
a[href='https://']
选择器,但可以在JavaScript中使用它,然后获取parentElement并将其设置为display: none
document.querySelectorAll('[href="https://"]').forEach(function(el) { el.parentElement.style.display = 'none'; });
<ul class="list-inline"> <li style="padding-right:20px;"> <a href="https://" rel="nofollow" target="_blank" title="{{channel.channel_name}} On FaceBook"><i aria-hidden="true" class="fa fa-facebook fa-2x" style='color:#ee6f00'></i> Facebook</a> </li> <li> <a href="https://twitter.com" rel="nofollow" target="_blank" title="{{channel.channel_name}} On Twitter"><i aria-hidden="true" class="fa fa-twitter fa-2x" style='color:#ee6f00'></i> Twitter</a> </li> </ul>
@LGSon is on the right track, but his answer only hides the hyperlink, not the li
parent of it. @LGSon是在正确的轨道上,但他的回答只是隐藏了超链接,而不是
li
的父母它。
This would do the trick: 这将达到目的:
// Get all the hyperlinks that don't have anything after "https://" into an array and loop the array Array.prototype.slice.call(document.querySelectorAll("a[href='https://']")).forEach(function(link){ link.parentElement.classList.add("hidden"); });
.hidden { display:none; } /* Add this class to any element that should be hidden */
<ul class="list-inline"> <li><a href="https://something">Link 1</a></li> <li><a href="https://">Link 2</a></li> </ul>
NOTES: 笔记:
.forEach()
is not universally supported on node lists in all current browsers, so converting the node list returned by .querySelectorAll()
is required to ensure cross-browser support. .forEach()
,因此需要转换.querySelectorAll()
返回的节点列表,以确保跨浏览器支持。 element.classList
property for easy access. element.classList
属性可轻松访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.