About jQuery append() and how to check if an element has been appended

前端 未结 6 1045
天命终不由人
天命终不由人 2021-01-11 10:21

I made a very simple button click event handler, I would like to have

element be appended when button clicked, you can check my code

6条回答
  •  既然无缘
    2021-01-11 11:08

    1. You are using mootools and not jQuery.

    2. To check if your element exists

    if($('#other').length > 0)

    So if you do not want to append the element twice:

    $("#search_btn").click(function() {
        if($('#other').length == 0) {
            $("#wrapper").append("

    I am here

    "); } });

    Or, you can use the .one(function)[doc]:

    $("#search_btn").one('click', function() {
        $("#wrapper").append("

    I am here

    "); });

提交回复
热议问题