Jquery SlideToggle 不同的 Div
今天我有这个问题。我在 php 中编写了一个简单的普通脚本,用于从数据库获取信息(在本例中为新闻),然后我想使用 Jquery 来切换新闻,但它没有按我想要的方式工作...实际上,当我单击时,我会这样做在新闻标题上,新闻内容将打开,然后如果您再次单击新闻标题,新闻内容将关闭...这是脚本:
<html>
<head>
<script type="text/javascript" src="../jquery/jquery-1.6.2.min.js"></script>
<script>
$(document).ready(function(){
$('.tit_news').click(function(){
$('.cont_news').slideToggle();
});
});
</script>
</head>
</html>
<?php
$query="SELECT * FROM tablename";
$do=mssql_query($query);
while($news=mssql_fetch_array($do)){
echo '<div class="tit_news">'.$news['title'].'</div><br /><div class="cont_news">'.$news['text'].'</div><hr>';
}
?>
但这有一个问题,当我单击新闻标题时,所有新闻都会显示已打开...我会只显示新闻内容单击将打开
today i have this problem. i wrote an easy normal script in php for take the information (in this case news) from a db, then i thinked to use Jquery for toggle news but it isn't working as i want... practically i would that when i click on a news title the news content will toggle on then if you click again the title of the news the news content will be toggle off... this is the script:
<html>
<head>
<script type="text/javascript" src="../jquery/jquery-1.6.2.min.js"></script>
<script>
$(document).ready(function(){
$('.tit_news').click(function(){
$('.cont_news').slideToggle();
});
});
</script>
</head>
</html>
<?php
$query="SELECT * FROM tablename";
$do=mssql_query($query);
while($news=mssql_fetch_array($do)){
echo '<div class="tit_news">'.$news['title'].'</div><br /><div class="cont_news">'.$news['text'].'</div><hr>';
}
?>
But this have the problem that when i click a news title ALL the news are toggle on...i would make that only the content of the news i click would toggle on
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 jQuery 更改为:
$('.cont_news') 匹配 .cont_news 的所有实例,而 $('.cont_news', this) 与 $(this).find('.cont_news') 相同。
Change your jQuery to this:
$('.cont_news') matches ALL instances of .cont_news, whereas $('.cont_news', this) is the same as $(this).find('.cont_news').