javascript - 使用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 个数组返回数据,而不是其中只有一个,这只会让我更加困惑..
解决方案
currid
,summaryName
并且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>
推荐阅读
- typescript - 如何从泛型函数(Typescript)中实现返回类型始终是可选的
- python - 如何构建调用其他 API 的 FastAPI 应用程序
- python - 我需要字符的共现数据框
- reactjs - React - 创建一个新的应用程序错误发布 8199 和令牌 \u000b
- asp.net-core - 队列未在 Rabbitmq 中显示
- java - 使用 D&C 算法对数组元素求和会给出错误的输出
- docker - Elastalert 与 Elasticsearch SSL
- python - Python - Dataframe.apply 多列
- sql - 我需要使用 sql 正则表达式函数提取代码字段的值
- powershell - 为什么cmd powershell -command中的最后一行错误