我想在ul class =" overl"中添加一些内容。已点击其父li的内容并删除任何以前被点击的li的innerhtml?
<html>
<head>
<link rel="stylesheet" type="text/css" href="testtab.css"/>
<script type="text/javascript" src="scripts/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function(){
$('#chat ul li a').on('click', function (e) {
($(this).innerHTML="");
//var i=$(this).closest('li').index();
});
});
</script>
</head>
<body>
<div id="chat" class="chat">
<ul class="chatmenu">
<li id="one" class="users"><a href="#one">abcy<span class="notify">1 new*</span></a>
<div class="chattng">
<ul class="overl">
</ul>
</div>
</li>
<li id="two" class="users"><a href="#two">avvv<span class="notify">a</span></a>
<div class="chattng">
<ul class="overl">
</ul>
</div>
</li>
<li id="three" class="users"><a href="#three">gtt<span class="notify">d</span></a>
</li>
<li id="four" class="users"><a href="#four">trt<span class="notify">e</span></a></li>
<li id="five" class="users"><a href="#five">kkk<span class="notify">f</span></a></li>
</ul>
</div>
</body>
</html>
一旦知道然后怎么做,无论点击哪个li,它的innerhtml都会获得一些东西,之前点击的li会将innerthtml设为null?
答案 0 :(得分:1)
您混淆了DOM
方法和jQuery
选择器:
$(this).innerHTML=""
必须
$(this).html("") // or $(this).empty()
然后,既然你要清理li
,那么你想要这样做:
$(this).parent().html("") // or $(this).parent().empty()
答案 1 :(得分:0)
尝试在click
$(document).ready();
像
$(document).ready(
function(){
$('.overl').html("");
$('#chat ul li a').on('click', function (e) {
$(this).parent().children('.chattng').children('.overl').html("Your message");
});
});