【问题标题】:I need my javascript code to retrieve id by block of code我需要我的 javascript 代码按代码块检索 id
【发布时间】:2015-06-24 15:01:28
【问题描述】:

所以我有一个可以从 onClick 检索 id 的 Javscript,但它只选择具有 id 的第一个 div。问题是我有多个在 php 中生成的唯一 ID,然后保存在 mysql 数据库中。 id 是唯一的,但我需要我的 onClick 来检索 div 块中的 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?

【问题讨论】:

  • 同时发布您的 HTML。而且我在您的代码中没有看到任何onclick
  • 可以描述“有没有办法为每个代码块选择id?”

标签: javascript php jquery html mysql


【解决方案1】:

我看到你的问题中有 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.
}

【讨论】:

  • 我喜欢你的代码,现在我正在尝试缩小范围,以便按钮只抓取同一个 div 中的 id 而不是其他的。
【解决方案2】:

使用 Jquery 会让生活更轻松。

var h=[];
$("div").each(function(){
   h.push($(this).attr('id'));
});

警报(h); 您将获得所有 div ID 的数组。

【讨论】:

    【解决方案3】:

    您需要获取元素,然后循环它们(当前您的循环代码不执行任何操作)

    function postFunction() {
    
        var postEls = document.getElementsByClassName('post'),
            postElsCount = postEls.length;
    
        for (var i = 0; i < postElsCount; i++) {
            alert(postEls[i].id);
        }
    
    }
    

    Here's a fiddle

    【讨论】:

      【解决方案4】:

      jQuery 将始终简化此类操作,但您也可以使用 vanilla javascript 实现相同的操作。由于跨浏览器对 javascript 的支持差异很大,因此需要付出努力和时间,但值得一试。

      function postFunction() {
          var ids = [];
          var x = document.getElementByClassName('post');
      
          for (var i = 0; i < x.length; i++) {
              var temp = x[i].getAttribute("id");
              ids.push(temp);
          }
          console.log(ids)
      }
      

      【讨论】:

        【解决方案5】:

        没有 jQuery:

        function postFunction() {
            var ids = Array.prototype.map.call(document.getElementsByClassName("post"), function(elem) {
                return elem.id
            });
            console.log(ids.join(", "));
        }
        

        【讨论】:

        • 在帖子中尝试过js 吗? document.getElementsByClassName("post") 是否返回一个可以链接到 Array.prototype.map() 方法的数组?
        • 你说得对,你需要array.prototype.map,编辑答案。
        【解决方案6】:

        getElementsByClassName() 返回具有提供的类名的所有 HTML 元素列表。在您的循环中,您只会提醒在索引 [0] 返回的 first 元素。

        试试:

        var x = document.getElementsByClassName("post");
        for (var i = 0; i < x.length; i = i + 1) {
            alert(x[i].getAttribute("id"));
        }
        <!DOCTYPE html>
        <html lang="en">
        <div id="blah1" class="post"></div>
        <div id="blah2" class="post"></div>
        <div id="blah3" class="post"></div>
        <div id="blah4" class="post"></div>
        <div id="blah5" class="post"></div>
        </html>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-01
          • 1970-01-01
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 2021-06-06
          • 1970-01-01
          相关资源
          最近更新 更多