如何获得网格卡上的ninternext点击?
请帮助我从网格卡中获得ninternef。
CSS文件;
.container1 {
display: grid;
grid-template-columns: auto auto auto auto;
column-gap: 10px;
justify-content: space-evenly;
}
.posts {
width: 13rem;
margin: 10px auto 10px auto;
height: 17rem;
background: white;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
border-radius: 10px;
}
<div class="container1">
<div class="posts">
<h3>Heading 1</h3>
</div>
<div class="posts">
<h3>Heading 2</h3>
</div>
<div class="posts">
<h3>Heading 3</h3>
</div>
<div class="posts">
<h3>Heading 4</h3>
</div>
<div class="posts">
<h3>Heading 5</h3>
</div>
<div class="posts">
<h3>Heading 6</h3>
</div>
</div>
如果我单击Grid3,依此类推,如何通过JavaScript获得直接的直观?
Please help me to get innerText of heading from grid card.
CSS File ;
.container1 {
display: grid;
grid-template-columns: auto auto auto auto;
column-gap: 10px;
justify-content: space-evenly;
}
.posts {
width: 13rem;
margin: 10px auto 10px auto;
height: 17rem;
background: white;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
border-radius: 10px;
}
<div class="container1">
<div class="posts">
<h3>Heading 1</h3>
</div>
<div class="posts">
<h3>Heading 2</h3>
</div>
<div class="posts">
<h3>Heading 3</h3>
</div>
<div class="posts">
<h3>Heading 4</h3>
</div>
<div class="posts">
<h3>Heading 5</h3>
</div>
<div class="posts">
<h3>Heading 6</h3>
</div>
</div>
How can I get innerText of heading if I click on grid3 and so on through Javascript ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是可能对您有帮助的代码。通过添加一个单击功能。
html:
JS:
参考链接
Here's the code which may help you. By add a onclick function.
HTML :
JS :
Reference Link stackblitz
假设网格参考
div.posts
元素,您可以只使用文档方法 queryselectorall()要检索所有网格的列表,请使用 foreach()方法通过检索到的网格列表进行迭代,然后将单击的侦听器添加到每个网格中,以检索标题元素内部文字。检查并运行以下代码段,以获取上述方法的实际示例:
Assuming grids refer to the
div.posts
elements, you can just use the the document method querySelectorAll() to retrieve a list of all the grids, use the forEach() method to iterate through the retrieved list of grids and then add a click listener to each grid that will retrieve the heading element's inner text.Check and run the following Code Snippet for a practical example of the above approach: