Using jQUery hide() on newly created dom elements

天涯浪子 提交于 2020-06-02 16:56:52

问题


I am relatively new to jQuery. What I have is an app that is displaying a list of content items from the database in a table.

If I click the title link of the page, it will load the page to edit in a tab (clicking teh tab lets me view that form to edit).

My problem is that in using .load and creating the html inside an empty div, my css that was previously hidden by jQuery is no longer being hidden in this new form.

For example, on the same page, I have a div with the class error. This is hidden. I click the title to edit, the page loads in an empty div, and the divs with error as a class are not hidden.

Is there a way around this to get these elements hidden?


回答1:


Here is the code that I always use when loading content from other url into a section of current page:

jQuery(function($){
  $("#edit").click(function(){
    //display loading message in target div, it's better have animation gif
    $('#target').html('<img src="PATH/loading.gif" alt="" /> Loading edit form...');
    //request the edit form using AJAX
    $.get(EDIT_FORM_URL,
      {},
      function(data){
        //display data to target div
        $('#target').html(data);
        //if the error div still displayed, do this
        $(".error").hide();
      });
  });
});

I never use .load. I prefer to use $.get and $.post instead for more control, but not as complex as $.ajax.

Hope this help. If you still get a weird result, post the code or URL here, so other SO member can help you more accurately.



来源:https://stackoverflow.com/questions/2061430/using-jquery-hide-on-newly-created-dom-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!