[英]Graying Out a Link when it is clicked using Jquery
Hello I am new to JQuery and have figured out how to make a list of divs that can be hidden and shown when a link is clicked.您好,我是 JQuery 的新手,并且已经知道如何制作一个可以在单击链接时隐藏和显示的 div 列表。 Now I would like the link that is currently clicked on to be grayed out.
现在我希望当前单击的链接变灰。 I am very unsure of how to do this.
我非常不确定如何做到这一点。
I am also new to slack overflow so if I did not this question right please let me know of I did not post enough of my code.我也是松弛溢出的新手,所以如果我没有正确回答这个问题,请告诉我我没有发布足够多的代码。
Jquery Jquery
jQuery(function () {
jQuery('#showall').click(function () {
jQuery('.targetDiv').show();
});
jQuery('.showSingle').click(function () {
jQuery('.targetDiv').hide();
jQuery('#div' + $(this).attr('target')).show();
});
});
Link Menu链接菜单
<a class="showSingle" target="1">Div1</a>
<h1 class="title">Heading</h1>
<a class="showSingle" target="2">Div2</a>
<a class="showSingle" target="3">Div3</a>
<a class="showSingle" target="4">Div4</a>
Divs To Be Toggled要切换的 div
<div id="div2" class="targetDiv" style="display:none">Div2</div>
<div id="div3" class="targetDiv" style="display:none">Div3</div>
<div id="div4" class="targetDiv" style="display:none">Div4</div>
Use $(this) to target the clicked link.使用 $(this) 来定位点击的链接。
jQuery(function () {
jQuery('#showall').click(function () {
jQuery('.targetDiv').show();
});
jQuery('.showSingle').click(function () {
jQuery('.targetDiv').hide();
jQuery(this).addClass('className');
jQuery('#div' + $(this).attr('target')).show();
});
});
In this way when a link is clicked class is added.这样,当点击链接时,class 就会被添加。
The below code changes the text color to grey下面的代码将文本颜色更改为灰色
jQuery(function () {
jQuery('#showall').click(function () {
jQuery('.targetDiv').show();
});
jQuery('.showSingle').click(function () {
jQuery('.targetDiv').hide();
jQuery('#div' + $(this).attr('target')).show();
jQuery('.showSingle').css("color", "#000");
jQuery(this).css("color", "grey");
});
});
simple and easy way to do it with data attribute使用数据属性的简单方法
$('.showSingle').click(function(e){ e.preventDefault(); let link_id = $(this).data('target'); $('.targetDiv[data-target="'+link_id+'"]').toggle(); })
.targetDiv{ background: red; height: 50px; width: 50px; display: inline-block; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="" class="showSingle" data-target="1">Div1</a> <h1 class="title">Heading</h1> <a href="" class="showSingle" data-target="2">Div2</a> <a href="" class="showSingle" data-target="3">Div3</a> <a href="" class="showSingle" data-target="4">Div4</a> <div data-target="1" class="targetDiv" style="display:none">Div1</div> <div data-target="2" class="targetDiv" style="display:none">Div2</div> <div data-target="3" class="targetDiv" style="display:none">Div3</div> <div data-target="4" class="targetDiv" style="display:none">Div4</div>
You can add/remove a CSS class to the element that is clicked.您可以将 CSS class 添加/删除到单击的元素。
jQuery(function () {
jQuery('#showall').click(function () {
jQuery('.targetDiv').show();
});
jQuery('.showSingle').click(function () {
jQuery('.selected').removeClass('selected');
jQuery('.targetDiv').hide();
jQuery('#div' + $(this).attr('target')).show();
jQuery(this).addClass('selected');
});
});
And this is the CSS:这是 CSS:
.selected { color: gray; }
Remember to always add role="button"
when using non-button elements as buttons (you're doing this by altering that default behavior of the anchor tags ).记住在使用非按钮元素作为按钮时始终添加
role="button"
(您通过更改锚标签的默认行为来做到这一点)。 Better yet, you could use actual buttons to achieve the same effect.更好的是,您可以使用实际按钮来实现相同的效果。 This is important for accessibility purposes (ADA, screen readers).
这对于可访问性目的(ADA、屏幕阅读器)很重要。 It is also a best-practice to use semantic HTML whenever possible.
尽可能使用语义 HTML 也是最佳实践。
<script>
jQuery(function () {
jQuery('#showall').click(function () {
jQuery('.targetDiv').show();
});
jQuery('.showSingle').click(function () {
jQuery('[disabled=true]').prop('disabled', false);
jQuery('.targetDiv').hide();
jQuery('#div' + $(this).attr('target')).show();
jQuery(this).prop('disabled', false);
});
});
</script>
<button class="showSingle" target="1">Div1</button>
<h1 class="title">Heading</h1>
<button class="showSingle" target="2">Div2</button>
<button class="showSingle" target="3">Div3</button>
<button class="showSingle" target="4">Div4</button>
<div id="div2" class="targetDiv" style="display:none">Div2</div>
<div id="div3" class="targetDiv" style="display:none">Div3</div>
<div id="div4" class="targetDiv" style="display:none">Div4</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.