Ale*_*ndr 7 html javascript css jquery
我有一些博客页面的HTML代码.下面 - 1个帖子的代码及其CSS缩减的高度.这将是博客页面上的很多帖子.我想通过点击"阅读更多"按钮查看特定页面的所有内容.带有博客内容的Div具有动态id,它通过PHP从数据库获取.
如何通过单击"阅读更多"按钮更改类"blog_article"的div的高度?
我想过使用JS/Jquery但是无法获得"blog_article"div的id.或者也许有更好的方法来做到这一点?
<div class="blog_article_wrapper">
<div class="blog_article" id="<?php echo $id; ?>">
<!--Some content-->
</div>
<div class="blog_article_read_more">
<button onclick="blogReadMore()">Read More</button>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但无法获取“blog_article”div 的 id
为什么你不能?:
<button onclick="blogReadMore(<?php echo $id; ?>)">Read More</button>
Run Code Online (Sandbox Code Playgroud)
或者,如果它是一个字符串:
<button onclick="blogReadMore('<?php echo $id; ?>')">Read More</button>
Run Code Online (Sandbox Code Playgroud)
然后blogReadMore()有一个参考id:
function blogReadMore(id) {
// use the id to identify the element and modify it however you want
}
Run Code Online (Sandbox Code Playgroud)
相反,由于您标记了 jQuery,因此您可以通过单击按钮遍历 DOM 来确定元素,而无需任何操作id。像这样的东西:
$('.blog_article_read_more button').click(function () {
var article = $(this).closest('.blog_article_wrapper').find('.blog_article');
// do whatever you like with the article
});
Run Code Online (Sandbox Code Playgroud)