使用Jquery按类搜索元素时获取空数组

时间:2019-04-07 03:27:42

标签: javascript jquery html

我想知道获取<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>

页面看起来像这样

link

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

link2

1 个答案:

答案 0 :(得分:1)

curridsummaryNametrend不是类名,而是名称属性。将选择器更改为.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>