gpt4 book ai didi

javascript - 我需要我的 javascript 代码来通过代码块检索 id

转载 作者:行者123 更新时间:2023-11-28 03:09:48 25 4
gpt4 key购买 nike

所以我有一个可以从 onClick 检索 id 的 Javscript,但它只选择第一个带有 id 的 div。问题是我有多个唯一的 id,它们是在 php 中生成的,然后保存在 mysql 数据库中。 ID 是唯一的,但我需要我的 onClick 来检索 div block 中的 ID。

 function postFunction() {
var i;
var x;
for (i = 0; i< x.length; i++)
x = document.getElementsByClassName("post")[0].getAttribute("id");
//alert(this.id);
alert(x);

}

有没有办法为每个代码块选择 id?

最佳答案

我看到您的问题中有 jQuery 标记。试试这个:

function postFunction() {
var ids = []; //in case you need to have all ids;
$('.post').each(function() {
var id = $(this).attr('id');
ids.push(id); //Store the id in the array
alert(id);
});
console.log(ids); //Show all ids.
}

关于javascript - 我需要我的 javascript 代码来通过代码块检索 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31030086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com