2013-10-13 123 views
1

未來某個元素我試圖隱藏負載對隱藏的jQuery

<script> 

$(document).ready(function() 
{ 
$.ajax({ 
    url:"http://localhost/test.php", 
    success:function(data){ 
      $('#div').html(data);} 
}); 

$('#btn').click(function(){ 
    $('#para').hide(); 
}) 


</script> 

<html> 

<div id="div"> </div> 
<input type="button" value="hide" id="btn"> 

</html> 

PHP

<?php 
echo"<p id='para'> this is test </p>" 
?> 

什麼我想是在客戶端上執行的未來元素的動作和我無法做到這一點...任何幫助.... ????

回答

3

您應該創建一個CSS規則,將其添加到將具有display:none的任何新元素。

這樣,任何新元素都會自動執行CSS規則。一旦你想顯示元素,只是刪除CSS類..

echo "<p id='para' class='hide'> this is test </p>" 
// here is the class------^ 

現在在你的CSS文件,你可以有這樣的:

.hide { 
    display: none; 
} 

要刪除類,使該段可見,使用這樣的:

$("#para").removeClass("hide"); 

您也可以通過jQuery添加這個類:

... 
success:function(data) { 
    var element = $(data); // assuming data is just raw HMTL 
    element.addClass("hide"); 
    $("#div").html(element.html()); 
} 
... 
+0

嗯,它確實隱藏了,但仍然有問題,使其再次可見 –

+0

您是否嘗試刪除「隱藏」類?你可以用這個選擇器匹配所有「隱藏的」元素 - '$(「.hide」)'。 – Lix

0

單擊#btn時,您可以嘗試設置標誌,然後在AJAX成功時隱藏段落(如果設置了標誌)。