JQuery 操作 Children 的 Div 集
JQuery Manipulate Children of Div Set
我正在创建一个从页面读取数据的 Greasemonkey 脚本 (JavaScript + JQuery)。我看到如下代码:
<div class="section">
<div class="history-giveaway-name">
<a href="www.google.com">Text!</a>
</div>
<div class="history-giveaway-state">
// More stuff here
</div>
</div>
<div class="section">
<div class="history-giveaway-name">
<a href="www.bing.com">Text!</a>
</div>
<div class="history-giveaway-state">
// More stuff here
</div>
</div>
我正在尝试从这些 div 中获取数据。我将 div 部分存储为列表,但我不知道如何获取 div,例如 "history-giveaway-name"。最终目标是从 children 获取数据,例如,从其中一个 href 获取 link。
这是我目前的情况,不确定哪里出错了。我收到此错误:“”,但检测到的始终是 3。JQuery 类型 () 之类的方法也不适用于部分 [x]。
var detected = 0;
var sections = $('[class$="section"]');
for (var i = 0; i < sections.length; i++) {
editDialogText(sections[i].type());
detected += 1;
}
使用 Jquery,您可以使用 .each
循环并执行如下操作:
var detected = 0;
// Simplify how you specify the section class
var sections = $('.section');
sections.each(function () {
// Get the URL within that section
var link = $(this).find('a').attr('href');
console.log(link);
detected +=1;
console.log(detected);
});
我不确定你想要达到什么目的。我在 jsfiddle 中复制了您的代码,没有收到任何错误:https://jsfiddle.net/fezhvm71/
这是我得到的:
Text!
// More stuff here
Text!
// More stuff here
我正在创建一个从页面读取数据的 Greasemonkey 脚本 (JavaScript + JQuery)。我看到如下代码:
<div class="section">
<div class="history-giveaway-name">
<a href="www.google.com">Text!</a>
</div>
<div class="history-giveaway-state">
// More stuff here
</div>
</div>
<div class="section">
<div class="history-giveaway-name">
<a href="www.bing.com">Text!</a>
</div>
<div class="history-giveaway-state">
// More stuff here
</div>
</div>
我正在尝试从这些 div 中获取数据。我将 div 部分存储为列表,但我不知道如何获取 div,例如 "history-giveaway-name"。最终目标是从 children 获取数据,例如,从其中一个 href 获取 link。
这是我目前的情况,不确定哪里出错了。我收到此错误:“”,但检测到的始终是 3。JQuery 类型 () 之类的方法也不适用于部分 [x]。
var detected = 0;
var sections = $('[class$="section"]');
for (var i = 0; i < sections.length; i++) {
editDialogText(sections[i].type());
detected += 1;
}
使用 Jquery,您可以使用 .each
循环并执行如下操作:
var detected = 0;
// Simplify how you specify the section class
var sections = $('.section');
sections.each(function () {
// Get the URL within that section
var link = $(this).find('a').attr('href');
console.log(link);
detected +=1;
console.log(detected);
});
我不确定你想要达到什么目的。我在 jsfiddle 中复制了您的代码,没有收到任何错误:https://jsfiddle.net/fezhvm71/
这是我得到的:
Text!
// More stuff here
Text!
// More stuff here