如何在打开课程后删除附加h2标签。我在努力,但没有工作

时间:2015-06-04 09:40:12

标签: javascript jquery

<ul class="nav" id="yui_3_17_2_3_1433409929526_470">
    <li class="dropdown open" id="yui_3_17_2_3_1433409929526_469">
        <a href="http://192.168.1.112/mth_old_31/moodle28/message/index.php?viewing=recentconversations" class="dropdown-toggle" data-toggle="dropdown" title="Unread messages (5)" id="yui_3_17_2_3_1433409929526_473">
            <span id="yui_3_17_2_3_1433409929526_472">5</span>
            <i class="fa fa-envelope"></i>
        </a>
        <ul class="dropdown-menu">
            <li class="open"><a title="Admin User will..." href="http://192.168.1.112/mth_old_31/moodle28/message/index.php?user1=2&amp;user2=4">
                <div class="message unread">    
                    <span class="msg-picture">
                        <img src="http://192.168.1.112/mth_old_31/moodle28/theme/image.php?theme=elfin28&amp;component=core&amp;image=u%2Ff1" alt="Picture of student user" title="Picture of student user" class="userpicture defaultuserpic" width="60" height="60">
                    </span>
                    <span class="msg-body">
                        <span class="msg-time">
                            <i class="fa fa-comments"></i>                                    
                        </span>
                        <span class="msg-sender">student</span>
                        <span class="msg-text">Admin User will...</span>
                    </span>
                </div>
            </a>
        </li>
    </ul>
    <h2 id="myid" class="box-messages">Massage</h2>
</li>
<div class="block_action" id="yui_3_17_2_3_1432698699754_495">
    <input type="image" class="moveto customcommand requiresjs" alt="Move this to the dock" title="Dock block" src="http://192.168.1.112/mth_old_31/moodle28/theme/elfin28/pix/arrow.png" id="yui_3_17_2_3_1432698699754_494">
</div>
</ul>

<script>
    $("#Box1 ul.nav li.dropdown").append('<h2 id="myid" class="box-messages">Massage</h2>');

    $( "#Box1 ul.nav li.dropdown.open a span" ).click(function() { $( "h2#myid" ).remove();});
</script>

我正在尝试这样但不起作用。我在哪里工作PLZ帮助

2 个答案:

答案 0 :(得分:3)

您需要使用event delegation,因为选择器必须稍后进行评估

$("#Box1 ul.nav").on('click', 'li.dropdown.open a span', function () {
    $("#myid").remove();
});

如果您使用$( "#Box1 ul.nav li.dropdown.open a span" )li没有open类,则点击处理程序将无法注册。

答案 1 :(得分:0)

您可以选择在“打开”时删除的链接。否则加上它。

jsfiddle.net/dJDHd/963/