jQuery - Check if DOM element already exists

前端 未结 10 1590
萌比男神i
萌比男神i 2020-12-04 21:01

I am trying to add some form elements dynamically via Ajax with jQuery. I want to make sure that I don\'t create the same element twice, so I only want to add it if it hasn\

10条回答
  •  温柔的废话
    2020-12-04 21:14

    No to compare anything, you can simply check that by this...,.

    if(document.getElementById("url")){ alert('exit');}
    if($("#url")){alert('exist');}
    

    you can also use the html() function as well like

    if($("#url).html()){alert('exist');}
    

提交回复
热议问题