gpt4 book ai didi

javascript - 基于点击事件用JQuery横向添加类

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:22:25 25 4
gpt4 key购买 nike

背景

我正在构建一个通知下拉列表,显示通知何时已读或未读。用户可以通过单击图标将通知标记为“已读”,从而将其变成一个复选标记和一个工具提示,该提示更改为选项以使其未读。所以,如果它是未读的,图标是黄色的,工具提示会显示“标记为已读”。如果用户单击它使其已读,图标将变为复选标记,变为绿色,工具提示将更改文本以将其标记为未读。所有这一切都在工作,但是,在 li 的左侧还有另一个图标,当用户单击我上面解释的图标时,我想将其切换为背景绿色。

我尝试了什么?

我尝试使用 .parent().siblings().find() 和其他方法,因为我几乎可以肯定它们将是我的解决方案,但不幸的是,我要么得到了所有的 to toggle,要么根本没有,或者正确的图标停止工作,等等。我显然没有正确构建我的函数参数。

我在中间 li 上硬编码了 bg-success 类,以向您展示我正在努力实现的目标。

所以,如果有人可以通过添加 bg-success 类来帮助我将那个 doggone left 图标的背景变成绿色,而它自己的 li 中的“已读/未读”图标被点击,我将不胜感激。

这是我的 CODEPEN

这也是我的代码:

HTML

<div class="container">
<header>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown open">
<a href="javascript:;" data-toggle="dropdown" class="dropdown-toggle has-notify" data-click="toggle-notify">
<i class="fa fa-bell"></i>
</a>
<ul class="dropdown-menu dropdown-notification pull-right">
<li class="dropdown-header">Notifications (5)</li>
<li class="notification-item">
<a href="javascript:;">
<div class="media"><i class="fa fa-exclamation-triangle"></i></div>
<div class="message"><p class="desc">Server down on 1/24/2016.</p></div>
<div class="option" data-toggle="tooltip" data-title="Mark as Read" data-click="set-message-status" data-status="unread" data-container="body"><i class="fa fa-circle-o"></i></div>
</a>
</li>
<li class="notification-item">
<a href="javascript:;">
<div class="media bg-success"><i class="fa fa-thumb-tack"></i></div>
<div class="message"><p class="desc">Approve documents in outbox.</p></div>
<div class="option read" data-toggle="tooltip" data-title="Mark as Unread" data-click="set-message-status" data-status="read" data-container="body"><i class="fa fa-circle-o"></i></div>
</a>
</li>
<li class="notification-item">
<a href="javascript:;">
<div class="media"><i class="fa fa-calendar-plus-o"></i></div>
<div class="message"><p class="desc">New event posted.</p></div>
<div class="option" data-toggle="tooltip" data-title="Mark as Read" data-click="set-message-status" data-status="read" data-container="body"><i class="fa fa-circle-o"></i></div>
</a>
</li>
</ul>
</li>
</ul>
</header>
</div>

CSS

.navbar-nav .open .dropdown-menu {
position: absolute;
background: #000;
border: 1px solid;
-webkit-box-shadow: 0 3px 8px rgba(0,0,0,0.25);
box-shadow: 0 3px 8px rgba(0,0,0,0.25);
}
.navbar .dropdown-menu.pull-right {
right: 0;
left: auto;
}
.navbar .dropdown-menu {
max-width: 360px;
left: 0;
}

.navbar-nav .open .dropdown-menu .dropdown-header {
padding: 5px 20px 8px;
border-bottom: 1px solid rgba(248,151,29,0.77);
color: rgba(248,151,29,0.77);
font-size: 15px;
font-weight: 100;
letter-spacing: 1px;
text-transform: uppercase;
font-family: "ABeeZee",sans-serif;
}
.dropdown-menu>li.dropdown-header {
padding: 5px 20px 8px;
border-bottom: 1px solid #bbb;
}

.dropdown-notification>li.notification-item {
position: relative;
}

.navbar-nav .open .dropdown-notification>li.notification-item>a {
padding: 15px 20px;
}
.dropdown-notification>li.notification-item a {
padding: 15px 20px;
}
.dropdown-menu>li>a {
font-size: 12px;
padding: 5px 20px;
color: #bbb;
}

.dropdown-menu>li>a:hover, .dropdown-menu>li>a:focus {
background: #101113;
color: #bbb;
}

.dropdown-notification>li.notification-item a:before,
.dropdown-notification>li.notification-item a:after {
content: '';
display: table;
clear: both;
}

.dropdown-notification>li.notification-item .media {
float: left;
width: 40px;
height: 40px;
overflow: hidden;
text-align: center;
line-height: 40px;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
-ms-border-radius: 50%;
border-radius: 50%;
background: #ccc;
color: #000;
}

.dropdown-notification>li.notification-item .media i {
font-size: 15px;
}

.dropdown-notification>li.notification-item .media+.message {
margin-left: 50px;
}
.dropdown-notification>li.notification-item .message {
padding-right: 20px;
}

.dropdown-notification>li.notification-item .desc {
font-size: 12px;
font-weight: 300;
margin-bottom: 3px;
color: #8f8f8f;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
p {
color: #bbb;
}
.dropdown-notification>li.notification-item .option {
position: absolute;
right: 10px;
top: 10px;
padding: 5px 10px;
font-size: 12px;
}
.dropdown-notification>li.notification-item .option .fa:before {
color: #F8971D;
}

.dropdown-notification>li.notification-item .option.read .fa:before {
content: '\f058';
color: #47a877;
}

.bg-success {
background: #47a877 !important;
}

JQUERY

$('[data-click="set-message-status"]').on('click', function (e) {
e.stopPropagation();
e.preventDefault();

var status = $(this).attr('data-status');
var tooltipText = 'Mark as Read';
if (status == 'read') {
$(this).removeClass('read');
//$('.notification-item').find('.media').removeClass('bg-success');
$(this).attr('data-status', 'unread');
//$('.notification-item').find('.media').attr('data-status', 'unread');
} else {
$(this).addClass('read');
//$('.notification-item').find('.media').addClass('bg-success');
$(this).attr('data-status', 'read');
//$('.notification-item').find('.media').attr('data-status', 'read');
tooltipText = 'Mark as Unread';
}
$(this).tooltip('hide').attr('data-original-title', tooltipText).tooltip('fixTitle');
});

谢谢!

最佳答案

使用 $(this).parents('a').find('.media') 定位点击图标“已读/未读”的 media 类并使用 addClass()/removeClass() 方法添加/删除类:

$(this).parents('a').find('.media').addClass('bg-success'); //add class
//And
$(this).parents('a').find('.media').removeClass('bg-success'); //remove class

希望这对您有所帮助。


片段

$('[data-click="set-message-status"]').on('click', function (e) {
e.stopPropagation();
e.preventDefault();

var status = $(this).attr('data-status');
var tooltipText = 'Mark as Read';
if (status == 'read') {
$(this).removeClass('read');
//$('.notification-item').find('.media').removeClass('bg-success');
$(this).attr('data-status', 'unread');
//$('.notification-item').find('.media').attr('data-status', 'unread');
$(this).parents('a').find('.media').removeClass('bg-success');
} else {
$(this).addClass('read');
//$('.notification-item').find('.media').addClass('bg-success');
$(this).attr('data-status', 'read');
//$('.notification-item').find('.media').attr('data-status', 'read');

$(this).parents('a').find('.media').addClass('bg-success');

tooltipText = 'Mark as Unread';
}
$(this).tooltip('hide').attr('data-original-title', tooltipText).tooltip('fixTitle');
});
	.navbar-nav .open .dropdown-menu {
position: absolute;
background: #000;
border: 1px solid;
-webkit-box-shadow: 0 3px 8px rgba(0,0,0,0.25);
box-shadow: 0 3px 8px rgba(0,0,0,0.25);
}
.navbar .dropdown-menu.pull-right {
right: 0;
left: auto;
}
.navbar .dropdown-menu {
max-width: 360px;
left: 0;
}

.navbar-nav .open .dropdown-menu .dropdown-header {
padding: 5px 20px 8px;
border-bottom: 1px solid rgba(248,151,29,0.77);
color: rgba(248,151,29,0.77);
font-size: 15px;
font-weight: 100;
letter-spacing: 1px;
text-transform: uppercase;
font-family: "ABeeZee",sans-serif;
}
.dropdown-menu>li.dropdown-header {
padding: 5px 20px 8px;
border-bottom: 1px solid #bbb;
}

.dropdown-notification>li.notification-item {
position: relative;
}

.navbar-nav .open .dropdown-notification>li.notification-item>a {
padding: 15px 20px;
}
.dropdown-notification>li.notification-item a {
padding: 15px 20px;
}
.dropdown-menu>li>a {
font-size: 12px;
padding: 5px 20px;
color: #bbb;
}

.dropdown-menu>li>a:hover, .dropdown-menu>li>a:focus {
background: #101113;
color: #bbb;
}

.dropdown-notification>li.notification-item a:before,
.dropdown-notification>li.notification-item a:after {
content: '';
display: table;
clear: both;
}

.dropdown-notification>li.notification-item .media {
float: left;
width: 40px;
height: 40px;
overflow: hidden;
text-align: center;
line-height: 40px;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
-ms-border-radius: 50%;
border-radius: 50%;
background: #ccc;
color: #000;
}

.dropdown-notification>li.notification-item .media i {
font-size: 15px;
}

.dropdown-notification>li.notification-item .media+.message {
margin-left: 50px;
}
.dropdown-notification>li.notification-item .message {
padding-right: 20px;
}

.dropdown-notification>li.notification-item .desc {
font-size: 12px;
font-weight: 300;
margin-bottom: 3px;
color: #8f8f8f;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
p {
color: #bbb;
}
.dropdown-notification>li.notification-item .option {
position: absolute;
right: 10px;
top: 10px;
padding: 5px 10px;
font-size: 12px;
}
.dropdown-notification>li.notification-item .option .fa:before {
color: #F8971D;
}

.dropdown-notification>li.notification-item .option.read .fa:before {
content: '\f058';
color: #47a877;
}

.bg-success {
background: #47a877 !important;
}
<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"/>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
<header>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown open">
<a href="javascript:;" data-toggle="dropdown" class="dropdown-toggle has-notify" data-click="toggle-notify">
<i class="fa fa-bell"></i>
</a>
<ul class="dropdown-menu dropdown-notification pull-right">
<li class="dropdown-header">Notifications (5)</li>
<li class="notification-item">
<a href="javascript:;">
<div class="media"><i class="fa fa-exclamation-triangle"></i></div>
<div class="message"><p class="desc">Server down on 1/24/2016.</p></div>
<div class="option" data-toggle="tooltip" data-title="Mark as Read" data-click="set-message-status" data-status="unread" data-container="body"><i class="fa fa-circle-o"></i></div>
</a>
</li>
<li class="notification-item">
<a href="javascript:;">
<div class="media bg-success"><i class="fa fa-thumb-tack"></i></div>
<div class="message"><p class="desc">Approve documents in outbox.</p></div>
<div class="option read" data-toggle="tooltip" data-title="Mark as Unread" data-click="set-message-status" data-status="read" data-container="body"><i class="fa fa-circle-o"></i></div>
</a>
</li>
<li class="notification-item">
<a href="javascript:;">
<div class="media"><i class="fa fa-calendar-plus-o"></i></div>
<div class="message"><p class="desc">New event posted.</p></div>
<div class="option" data-toggle="tooltip" data-title="Mark as Read" data-click="set-message-status" data-status="read" data-container="body"><i class="fa fa-circle-o"></i></div>
</a>
</li>
</ul>
</li>
</ul>
</header>
</div>

关于javascript - 基于点击事件用JQuery横向添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34681260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com