我想知道如何获取<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>
关于javascript - 使用 Jquery 按类搜索元素时获取空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555347/