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

标签 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

最佳答案

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>

关于javascript - 使用 Jquery 按类搜索元素时获取空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555347/

相关文章:

html - CSS3动画(关键帧动画)

javascript - 如何在父 div 折叠时删除 OnClicked 类?

javascript - 父div之外的类的Jquery最接近方法

jquery - 使用 -webkit-overflow-scrolling : touch; 获取准确的滚动位置

javascript - 如何从 Jquery/JS 中的文本文件中读取数据以将其显示在 html 页面中?

javascript - 相关下拉错误

javascript - 根据选择传递值

javascript - 来自 Controller 外部的 Angular JS 访问范围

javascript - 组件无法跟上 redux 状态

html - 如何更改输入边框颜色