Javascript jquery hide在ajax调用后不适用于同一个div类?

Javascript jquery hide在ajax调用后不适用于同一个div类?,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,1.这是我的代码我有一个div类内部,它是使用ajax调用动态加载的 在ajax调用之后,如果我单击隐藏按钮,它将不工作 但它在ajax请求之前工作得很好 因此,为了克服这个问题,我只需添加一个外部div并隐藏该div,这次它就可以工作了。。 我不知道为什么 $( "#inner" ).replaceWith( data ); /*and*/ $( "#inner" ).hide(); //not working $( "#inner" ).replaceWith( data );

1.这是我的代码我有一个div类内部,它是使用ajax调用动态加载的 在ajax调用之后,如果我单击隐藏按钮,它将不工作

但它在ajax请求之前工作得很好

因此,为了克服这个问题,我只需添加一个外部div并隐藏该div,这次它就可以工作了。。 我不知道为什么

$( "#inner" ).replaceWith( data );  /*and*/  $( "#inner" ).hide();   //not working


$( "#inner" ).replaceWith( data );    /*and*/     $( "#outer" ).hide();  //working
为什么我们不能使用相同的div类

<html>
  <div id="outer">
  <div id="inner">
    <br /> <br /> <br />
  <div>  <input type="button" value="signup"  onclick="changeval();"/>
  </div>
  <br /> <br />  
  </div>
  </div>
  <input type="button" value="hide" onclick="onhide();"/>    
  <script language="javascript"> 
   function changeval(context)
   {          
     var typeval="sdsf";
     var url="sdfsdf";
     $.ajax({
        type:'POST',
        url:'htp://sscs/registration',
        data:'&typeval='+typeval+'&url='+url,
        success:function(data) { 
          $( "#inner" ).replaceWith( data );           
        }        
      });         
    }
    function onhide()
    {
      $( "#inner" ).hide();
    }
  </script>






函数changeval(上下文) { var typeval=“sdsf”; var url=“sdfsdf”; $.ajax({ 类型:'POST', 网址:'htp://sscs/registration', 数据:'&typeval='+typeval+'&url='+url, 成功:函数(数据){ $(“#内部”)。替换为(数据); } }); } 函数onhide() { $(“#内部”).hide(); }
像这样使用:

$( "#inner" ).replaceWith( function(){
    return $(this).data();
} );
在ajax调用之后,#内部div不再存在。您将用ajax请求的响应替换它

您可以使用
$(“#inner”).html(数据)
保留div,然后在收到响应后将其隐藏。

使用

而不是作为

用提供的新内容替换匹配元素集中的每个元素,并返回已删除的元素集


,在这里您可以看到第二类的div被替换为输入内容。

它不起作用,因为您替换了


包括div及其ID。
保持不变,因此您的其他隐藏工作正常,它仍然会发现
div

错误的上下文,抱歉。应该是html()。尝试使用$(“#内部”)。替换为($(“#内部”,数据));尝试用.html()代替replaceWith()是的,它可以工作,但我想知道为什么我们使用replace时它不工作
 $("#inner").html(data);