如何将innerhtml添加到点击其父li的特定ul?

时间:2013-06-22 13:08:24

标签: javascript jquery

我想在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?

2 个答案:

答案 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");
   });
  });