如何使用两个'onclick'事件

Ali*_*eza 3 javascript

下面是我的代码,当我点击附加文本时,为什么它不起作用?

1:点击这是一个段落.

2:单击附加文本

3:必须显示红色的附加项目.

$(document).ready(function(){
    $(".ali").click(function(){
        $(this).parent().append("<b class='reza'>Appended text</b>");
    });
    $(".reza").click(function(){
        $(this).append("<li style='color:red'>Appended item</li>");
    });
});
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

    <p><span class="ali"> This is a paragraph. </span> </p>


</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Rad*_*eda 6

由于尚未创建具有"reza"类的元素,因此需要使用"reze"类在future元素上定义click事件.检查以下工作代码.

$(document).ready(function(){
    $(".ali").click(function(){
        $(this).parent().append("<b class='reza'>Appended text</b>");
    });
    $("body").on("click",".reza",function(){
        $(this).append("<li style='color:red'>Appended item</li>");
    });
});
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

<p><span class="ali"> This is a paragraph. </span> </p>


</body>
</html>
Run Code Online (Sandbox Code Playgroud)