首页 > 解决方案 > 使用Jquery按类搜索元素时获取空数组

问题描述

我想知道获取<a>标签文本的方法,一直在尝试不同的方法,但到目前为止没有成功

我搜索了多种获取值的方法,使用 document.getElementsByClassName() 或 document.getElementsByName() 但数组始终返回空

<!DOCTYPE html>
<meta charset="utf-8"/>
<html lang="en">
<head>
<title>Currencies</title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>

<body>
    <input id="clickMe" type="button" value="clickme" onclick="saveData();" />

    </br>
    <div class ="currencies">
    <a class="mypanel"></a>
    </div>
    <script>
    var text = ``
    function getData(){

        $.getJSON('https://cors-anywhere.herokuapp.com/http://www.investing.com/common/technical_summary/api.php?action=TSB_updatePairs&pairs=1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,47,48,49,50,52,53,54,55&timeframe=300', 
            function(data) {
                var x=0
                for(i=0; i<60;i++){
                try{
                text = text + `Currency_id : <a name="currid">${i}</a><br>
                               Value: <a class="summaryLast">${data[i].summaryLast}</a><br>
                            Currency: <a name="summaryName">${data[i].summaryName}</a><br>
                            Trend: <a name="trend">${data[i].technicalSummary}</a><br><br>`

                }

                catch(e){

                }

                }
                $(".mypanel").html(text);

            });

    }

    getData()

    function saveData(){

        var currency_ids = []
        $('.mypanel > a.currid').each(function(i){
          currency_ids[i] = $(this).text()
        })
        console.log(JSON.stringify(currency_ids))

        var currency_values = []
        $('.mypanel > a.summaryLast').each(function(i){
          currency_values[i] = $(this).text()
        })
        console.log(JSON.stringify(currency_values))

        var currency_name = []
        $('.mypanel > a.summaryName').each(function(i){
          currency_name[i] = $(this).text()
        })
        console.log(JSON.stringify(currency_name))
        var currency_trend = []
        $('.mypanel > a.trend').each(function(i){
          currency_trend[i] = $(this).text()
        })
        console.log(JSON.stringify(currency_trend))

    }



    </script>

</body>
</html>

页面看起来像这样

关联

当我单击按钮时,我希望 4 个数组返回数据,而不是其中只有一个,这只会让我更加困惑..

链接2

标签: javascriptjqueryhtml

解决方案


curridsummaryName并且trend不是类名而是名称属性。将您的选择器更改为.mypanel > a[name="your_name_value"]

var text = "";

function getData() {
  $.getJSON('https://cors-anywhere.herokuapp.com/http://www.investing.com/common/technical_summary/api.php?action=TSB_updatePairs&pairs=1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,47,48,49,50,52,53,54,55&timeframe=300',
    function(data) {
      var x = 0
      for (i = 0; i < 60; i++) {
        try {
          text = text + `Currency_id : <a name="currid">${i}</a><br>
                               Value: <a class="summaryLast">${data[i].summaryLast}</a><br>
                            Currency: <a name="summaryName">${data[i].summaryName}</a><br>
                            Trend: <a name="trend">${data[i].technicalSummary}</a><br><br>`
        } catch (e) {}
      }
      $(".mypanel").html(text);
    });
}

getData()

function saveData() {

  var currency_ids = []
  $('.mypanel > a[name="currid"]').each(function(i) {
    currency_ids[i] = $(this).text()
  })
  console.log(JSON.stringify(currency_ids))

  var currency_values = []
  $('.mypanel > a.summaryLast').each(function(i) {
    currency_values[i] = $(this).text()
  })
  console.log(JSON.stringify(currency_values))

  var currency_name = []
  $('.mypanel > a[name="summaryName"]').each(function(i) {
    currency_name[i] = $(this).text()
  })
  console.log(JSON.stringify(currency_name))
  var currency_trend = []
  $('.mypanel > a[name="trend"]').each(function(i) {
    currency_trend[i] = $(this).text()
  })
  console.log(JSON.stringify(currency_trend))

}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<input id="clickMe" type="button" value="clickme" onclick="saveData();" />

</br>
<div class="currencies">
  <a class="mypanel"></a>
</div>


推荐阅读