我需要更新两个数据值。
当我点击带有类.messageconv的div时,抓取data-unread_mess_n值并从data-menutop_mess_nr和相同的data-unread_mess_n中减去它。
如果data-menutop_mess_nr == 0的值改变了类。
我无法更新data-unread_mess_n和data-menutop_mess值为什么?
$(document).on('click touchstart', '.messageconv', function(e) {
e.preventDefault();
messagesNumber($(this));
});
function messagesNumber(elem) {
var numMess = elem.parent().data('unread_mess_n');
var numMessMenutop = $('#menutop_mess_nr').data('menutop_mess_nr');
if (numMess != 0) {
elem.children('p').remove();
}
var remainingMess = numMessMenutop - numMess;
$('#menutop_mess_nr').html(remainingMess);
if (remainingMess == 0) {
$('#menutop_mess_nr').addClass('bluecount').removeClass('redcount');
}
elem.parent().data('unread_mess_n','0');
$('#menutop_mess_nr').data('menutop_mess_nr',remainingMess);
}
.messageconv{
border: 2px solid red;
height: 50px;
}
.redcount {
background: #de2424;
width:30px;
}
.bluecount {
background: #333399;
width:30px;
}
.redcount:hover {
background: #ed4747;
text-decoration: none;
}
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<a data-unread_mess_n="2" href="5">
<div class="messageconv">
test
<p class="incmessage redcount">2</p></div>
</a>
<a data-unread_mess_n="3" href="4">
<div class="messageconv">
test1
<p class="incmessage redcount">3</p></div>
</a>
<p data-menutop_mess_nr="5" id="menutop_mess_nr" class="incmessage redcount">5</p>
答案 0 :(得分:0)
你可以试试这个
elem.parent().attr('unread_mess_n','0');