[英]<ul> <li> Hide/show inner text only not <i> icon tag
In jQuery I need to hide/show inner text of anchor tag. 在jQuery中,我需要隐藏/显示锚标记的内部文本。 Here is html : 这是html:
<div id="side_bar">
<ul>
<li><a href="#"><i class="fa fa-fw fa-cog"></i>General Settings</a></li>
<li><a href="#"><i class="fa fa-fw fa-cog"></i>Other Settings</a></li>
<li><a href="#"><i class="fa fa-fw fa-user"></i>User</a></li>
</ul>
</div>
on a button click I need to hide/show only inner text General Settings
, Settings
, User
. 在按钮上单击我只需要隐藏/显示内部文本General Settings
, Settings
, User
。
I tried this : 我尝试了这个:
$.each($('#side_bar').find('ul').find('li'), function (key, value) {
$(this).find('a').contents().get(0).nodeValue.hide();
});
but it's not working. 但它不起作用。
got this error in console :: 在控制台中收到此错误::
TypeError: $(...).find(...).contents(...).get(...).nodeValue is null
$(this).find('a').contents().get(0).nodeValue.hide();
I need result to only hide show <a>
tag's inner text, not the icon. 我只需要隐藏show <a>
标签的内部文本而不是图标的结果。 I try to make collapse sidebar menu. 我尝试制作折叠边栏菜单。
NOTE
:: I must not need to add <span>
in to menu. NOTE
:::我不必在菜单中添加<span>
。
You could wrap you text by span
's and use simple selector like : 您可以用span
包裹文本,并使用简单的选择器,例如:
$('#side_bar li a span')
Hope this helps. 希望这可以帮助。
$('#toggle-display').on('click', function(){ $('#side_bar li a span').toggle(); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/> <button id='toggle-display'>Toggle display</button> <div id="side_bar"> <ul> <li><a href="#"><i class="fa fa-fw fa-cog"></i><span>General Settings</span></a></li> <li><a href="#"><i class="fa fa-fw fa-cog"></i><span>Other Settings</span></a></li> <li><a href="#"><i class="fa fa-fw fa-user"></i><span>User</span></a></li> </ul> </div>
Here's a solution strictly based on your existing description. 严格根据您现有的描述,这是一个解决方案。
First there is a button, with a click event handler, since you say you want this to happen on a button click. 首先,有一个带有click事件处理程序的按钮,因为您说您希望在按钮单击时发生这种情况。 Second, you only want to hide the "inner text". 其次,您只想隐藏“内部文本”。 I take this to mean you don't want to hide the <i>
tag. 我的意思是您不想隐藏<i>
标签。 The easiest way is to put the text into a span with a class, then it's straightforward to locate - all the find
stuff is completely unnecessary, you can use a simple selector: 最简单的方法是将文本与一个类放到一个范围内,然后可以直接查找-完全不需要所有find
内容,您可以使用一个简单的选择器:
<div id="side_bar">
<ul>
<li><a href="#"><i class="fa fa-fw fa-cog"></i><span class="menuText">General Settings</span></a></li>
<li><a href="#"><i class="fa fa-fw fa-cog"></i><span class="menuText">Other Settings</span></a></li>
<li><a href="#"><i class="fa fa-fw fa-user"></i><span class="menuText">User</span></a></li>
</ul>
</div>
<button id="myButton" type="button">Click to hide</button>
$(function() {
$("#myButton").click(function() {
$("#side_bar li a .menuText").hide();
});
});
OR, as per your last update, you're certain you can't/won't add a <span>
into the markup, you can just hide the text. 或者,根据您的上一次更新,您确定无法/不会在标记中添加<span>
,您可以隐藏文本。 I've also included a feature to save and restore the text later when the button is clicked, by keeping the text in a data-
attribute of the anchor: 我还提供了一项功能,该功能可在以后单击按钮时通过将文本保留在锚点的data-
属性中来保存和恢复文本:
<div id="side_bar">
<ul>
<li><a href="#"><i class="fa fa-fw fa-cog"></i>General Settings</a></li>
<li><a href="#"><i class="fa fa-fw fa-cog"></i>Other Settings</a></li>
<li><a href="#"><i class="fa fa-fw fa-user"></i>User</a></li>
</ul>
</div>
<button id="myButton" type="button">Click to hide</button>
$(function() {
$("#myButton").click(function() {
$("#side_bar li a").each(function() {
var el = $(this);
if (el.text() != '') {
el.data('originalText', el.text());
el.text('');
}
else {
el.text(el.data('originalText'));
}
});
});
});
Working fiddle here: http://jsfiddle.net/xcxv682h/1/ 在这里工作的小提琴: http : //jsfiddle.net/xcxv682h/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.