0

我创建了一个列表页面,其中每个列表都有自己的 div,该 div 的内容是通过 $.post 从单独的 php 脚本加载的

我遇到的问题是试图隐藏列表,我认为问题可能与这条线有关

$('div.close_listing')

只有我使用它才能让它工作

$('div.listing> div').click(  
function() {
$('div.listing> div').hide(); 
});

(当我单击列表 div 中的任意位置时有效)

非常感谢任何帮助。

清单的 html 代码是:

<div id='listing'>
   <div id='loading'></div>
   <a name='13'><h3>Business1</h3></a> <h4 data-id=18-3-3>Name Of Business 1</h4>
    <div>
        <hr>
     *** content forbusiness1 appears here ***
          <div class='close_listing'>CLOSE this listing</div>
    </div>

   <a name='14'><h3>Business2</h3></a> <h4 data-id=19-3-3>Name Of Business 2</h4>
    <div>
        <hr>
     *** content for business2 appears here ***
          <div class='close_listing'>CLOSE this listing</div>
    </div>

</div>

调用内容的代码如下:

$(document).ready(function() { 
$('div.listing> div').hide();  
$('div.listing> h4').click(function() {
$('div.listing> div').empty().html('<img src="loading_image.gif" /><br>Retrieving Details.....');
$.post("http://www.example.com/record.php", { id: $(this).attr('data-id') });

$.post('show.php',{ id: $(this).attr('data-id')}, function(data) {
$('div.listing> div').html(data); 

$('div.listing .close').visible();
});

var $nextDiv = $(this).next();
var $visibleSiblings = $nextDiv.siblings('div:visible');

if ($visibleSiblings.length ) {
  $visibleSiblings.slideUp('fast', function() {
    $nextDiv.slideToggle('fast');
  });
} else {
   $nextDiv.slideToggle('fast');
} 
});

// closes the listing down

$('div.close_listing').click(  
function() {
$('div.listing> div').hide(); 
}); 

});
4

3 回答 3

1

我猜你打算这样做

$('div.close_listing').click(function() {
        $(this).closest('.listing').hide(); 
    }); 
});
于 2012-05-29T16:20:38.507 回答
0

我发现您的问题有点难以理解,但是如果您想要关闭 close_listing 父 div,那么我已经像这样编辑了您的 html 标记

 <div id='listing'>
   <div id='loading'></div>
   <a name='13'><h3>Business1</h3></a> <h4 data-id=18-3-3>Name Of Business 1</h4>
    <div>
        <hr>
     content forbusiness1 appears here
          <a class='close_listing'  href="#">
            CLOSE this listing
          </a>
    </div>

   <a name='14'><h3>Business2</h3></a> <h4 data-id=19-3-3>Name Of Business 2</h4>
    <div>
        <hr>
      content for business2 appears here

           <a class='close_listing'  href="#">
            CLOSE this listing
          </a>
    </div>

</div>

javascript应该是这样的

$('a.close_listing').bind('click', function () {

    $(this).parent().show().fadeOut(500);
            return false;
    });
于 2012-05-29T16:41:35.933 回答
0

问题是这一行:

$('div.listing> div').hide(); 

它应该是

$('div#listing > div').hide();

但我认为你隐藏了很多(#listing 正下方的所有 div)。考虑以下:

$(this).parent().hide();
于 2012-05-29T16:22:46.913 回答