【问题标题】:How to extract name from a button (jQuery navigation)?如何从按钮中提取名称(jQuery 导航)?
【发布时间】:2020-08-08 14:06:56
【问题描述】:

特定网站上的部分源代码如下所示:

                                    .
                                    .
                                    .

                       <div class="list-group-item">
        <strong>item 1</strong>
        description
        <a class="btn btn-default btn-xs pull-right listing-sub-remove"
           data-id="5e98a2dc5e0aye57597b181b">
            Unsubscribe
        </a>
    </div>
                        <div class="list-group-item">
        <strong>item 2</strong>
        description
        <a class="btn btn-default btn-xs pull-right listing-sub-remove"
           data-id="5e58ac128edaey3f2935d94b">
            Unsubscribe
        </a>
    </div>
                                    .
                                    .
                                    .

如何获取每个 data-id 值?

我试过了

      $('.list-group-item').each(function(){
        var text = ($(this).text());
        console.log(text);
      });

但它返回 item 1(2)、description 和 Unsubscribe 文本。

我也试过

      $('.btn btn-default btn-xs pull-right listing-sub-remove').each(function(){
        var text2 = ($(this).text());
        console.log(text2);
      });

但这根本不起作用。 我应该使用什么命令来成功获取 data-id 值?

【问题讨论】:

    标签: javascript jquery parsing dom web-scraping


    【解决方案1】:

    你可以这样做:

    $(document).ready(function() {
       $(".btn.btn-default.btn-xs.pull-right.listing-sub-remove").each(function() {
       console.log($(this).data("id"));
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
         <div class="list-group-item">
           <strong>item 1</strong>
           description
           <a class="btn btn-default btn-xs pull-right listing-sub-remove" data-id="5e98a2dc5e0aye57597b181b">
             Unsubscribe
           </a>
         </div>
         <div class="list-group-item">
           <strong>item 2</strong>
           description
           <a class="btn btn-default btn-xs pull-right listing-sub-remove" data-id="5e58ac128edaey3f2935d94b">
             Unsubscribe
           </a>
         </div>

    【讨论】:

    • 谢谢,但我如何从btn btn-default btn-xs pull-right listing-sub-remove 中准确获取数据?页面上还有几个class="btn btn-footer"会弄乱结果
    • @narra_kk 我刚刚调整了我对这个要求的回答:)
    • @narra_kk 不幸的是没有。我自己从来没有读过指南,并且学到了我在生产中学到的所有东西。但我想您可以使用 Google 轻松找到在线教程。也许一个好的开始是learn.jquery.com?
    • @narra_kk 顺便说一句,您可能对此感兴趣 - 而不是 $(this).data("id") 我可以写 $(this).attr("data-id") 和得到相同的结果。如果您使用 data() 读取数据属性,则省略属性名称的“data-”部分,因此它只是 data(“id”)。
    • @narra_kk 关于您的选择器问题,要了解更多信息,您只需在 Google 上搜索“MDN,css 选择器”即可。 jQuery 选择器符合匹配 css 选择器,在 api.jquery.com/category/selectors 中详细介绍了一些额外的附加功能
    【解决方案2】:

    这就是你要找的吗?

    // We gather ALL elements with '.list-group-item' class
    var elements = $(".list-group-item");
      // We iterate over each element in the array of elements
      $.each(elements, function(index, element) {
      // We create a variable that takes the child link anchor [a tag], and grabs the data-id attribute from it
      var dataId = $(element).find("a")[0].dataset.id;
      // If the variable is not undefined, it will console log the result
      if (dataId) { console.log(dataId); };
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="list-group-item">
      <strong>item 1</strong> description
      <a class="btn btn-default btn-xs pull-right listing-sub-remove" data-id="5e98a2dc5e0aye57597b181b">
                Unsubscribe
            </a>
    </div>
    <div class="list-group-item">
      <strong>item 2</strong> description
      <a class="btn btn-default btn-xs pull-right listing-sub-remove" data-id="5e58ac128edaey3f2935d94b">
                Unsubscribe
            </a>
    </div>
    .

    【讨论】:

    • 据我所知,find("a") 找到了这个字符串:&lt;a class="btn btn-default btn-xs pull-right listing-sub-remove" data-id="5e58ac128edaey3f2935d94b"&gt; Unsubscribe &lt;/a&gt; 如果字符串中没有 dataset 单词,dataset.id 如何工作?
    • 我可以很容易地修改添加条件检查。
    • 我将添加 cmets 以使其更清晰。道歉。
    • 当我最初查看它时,您似乎想要定位列表组项,而不仅仅是链接本身。这就是为什么我走我的路。直接定位锚标记是一种更直接的方法,似乎适合您的用例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2013-05-07
    • 2016-06-12
    • 1970-01-01
    • 2011-12-13
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多