Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取<;a>;在元素中找到href?_Javascript_Jquery - Fatal编程技术网

Javascript 如何获取<;a>;在元素中找到href?

Javascript 如何获取<;a>;在元素中找到href?,javascript,jquery,Javascript,Jquery,给定如下html: <tr> <th style="padding-right:1em">Location</th> <td> <span class="location">Lower reaches of the <a href="/wiki/Geum_River" title="Geum River">Geum River</a>, <a href="/wiki/Kore

给定如下html:

<tr>
    <th style="padding-right:1em">Location</th>
    <td>
        <span class="location">Lower reaches of the <a href="/wiki/Geum_River" title="Geum River">Geum River</a>, <a href="/wiki/Korea" title="Korea">Korea</a></span>
    </td>
</tr>

我正在从字符串中拆分
/wiki/
,它可以工作,但我只得到了第一个
Geum_River
,而我希望所有
s
href

首先,我要在span元素上弹出一个
id
,以便在脚本中轻松找到它


您只选择了第一个

您的主要问题是调用
countryLinks=doSelect(“Location”).sibbins('td').find('a').attr('href')
特别是,当您调用最后一位
.attr('href')

描述:获取匹配元素集中第一个元素的属性值

所以基本上,您将获得一个链接集合,然后将该集合缩减为第一个元素并返回其href属性

以下是我将如何使用以下方法:
var$wikiDOM=$('.some container');
var links=$.map($wikiDOM.find('td a'),函数(link,i){
返回(link.href | |“)。替换(/^.[\\/]/,”);
});
控制台日志(链接)

位置
长江下游,,

这并不像我想的那么容易:-)

但我们要做的是: //缓存元素时,可以执行QuerySelectorAll并遍历 //节点列表,但让我们保持简单 var ts=document.querySelector('a')

可以使用jqueryeach()获取数组中的所有HREF,然后使用for循环逐个显示。 代码如下:

var hrefs = new Array();

jQuery('.location').find('a').each(function() {
    hrefs.push(jQuery(this).attr('href'));
});

function countryList() {
  let pattern = new RegExp('\/wiki\/');
for(var i=0; i < hrefs.length ; i++){
  string = hrefs[i];
  var countryListLinks = string.replace(pattern, '');
  alert(countryListLinks);
}
}

countryList();
var hrefs=new Array();
jQuery('.location')。find('a')。each(function(){
push(jQuery(this.attr('href'));
});
函数countryList(){
let pattern=newregexp('\/wiki\/');
对于(var i=0;i
完整的代码,应该是这样的:

function doSelect(text) {
  return $wikiDOM.find(".infobox th").filter(function() {
    return $(this).text() === text;
  });
}

var hrefs = new Array();

jQuery('.location').find('a').each(function() {
    hrefs.push(jQuery(this).attr('href'));
});
function countryList() {
  let pattern = new RegExp('\/wiki\/');
for(var i=0; i < hrefs.length ; i++){
  string = hrefs[i];
  var countryListLinks = string.replace(pattern, '');
  console.log(countryListLinks);
}
}
if (doSelect('Location').length > 0 && doSelect('Date').length > 0) {
  countryList();
};
函数doSelect(文本){
返回$wikiDOM.find(“.infobox th”).filter(函数(){
返回$(this).text()==文本;
});
}
var hrefs=新数组();
jQuery('.location')。find('a')。each(function(){
push(jQuery(this.attr('href'));
});
函数countryList(){
let pattern=newregexp('\/wiki\/');
对于(var i=0;i0和&doSelect('Date')。长度>0){
countryList();
};
var hrefs=new Array();
jQuery('.location')。find('a')。each(function(){
push(jQuery(this.attr('href'));
});
函数countryList(){
let pattern=newregexp('\/wiki\/');
对于(var i=0;i

位置
长江下游,,

什么是
doSelect
?@bleeted0d用doSelect函数更新很酷,但什么是
$wikiDOM
?@bleeted0d这是我通过api获得的整个页面。问题是这个列表,与doSelect、api和内容无关。这是一个列表问题所以,您只想返回title属性?你有到页面的链接吗?这将从每个链接中获取“filename”值,也就是说最后一个
/
之后的值,如果你不想这样,只需将正则表达式更改回你拥有的
'/wiki/'
,这并不能真正解决实际问题,即OP只获取两个链接中的一个。这个问题与元素选择有关,而不是字符串操作来吧,然后使用QuerySelectorAll并在其上循环,问题通过两个链接之一得到解决。他应该能够找出其余的。整个问题是OP通过调用
.attr()
来减少集合,该函数获取匹配元素集中第一个元素的属性值。他们最初从href获取所需字符串的方法非常好。您所做的只是建议一种替代方法来执行OP代码中的一部分,该部分确实有效。您好,谢谢,这很有希望,但我只得到第二部分,Korea,当我执行
console.log(countryLinks.forEach(countryList))undefined
@rob.m
返回
countryListLinks
from
countryList
函数,并在
if
语句
控制台.log(countryLinks.map(countryList))中使用
.map()
现在要尝试吗
// This is caching the href attribute from the link
var garbage = ts.href;

// here you take the link and run the native string method 
// to find the lastIndexOf, this is great if you suck at regex like myself
var n = garbage.lastIndexOf('/');

Here we extract only the part after lastIndexOf and cache it in "result"
var result = garbage.substring(n + 1);
alert(result);
var hrefs = new Array();

jQuery('.location').find('a').each(function() {
    hrefs.push(jQuery(this).attr('href'));
});

function countryList() {
  let pattern = new RegExp('\/wiki\/');
for(var i=0; i < hrefs.length ; i++){
  string = hrefs[i];
  var countryListLinks = string.replace(pattern, '');
  alert(countryListLinks);
}
}

countryList();
function doSelect(text) {
  return $wikiDOM.find(".infobox th").filter(function() {
    return $(this).text() === text;
  });
}

var hrefs = new Array();

jQuery('.location').find('a').each(function() {
    hrefs.push(jQuery(this).attr('href'));
});
function countryList() {
  let pattern = new RegExp('\/wiki\/');
for(var i=0; i < hrefs.length ; i++){
  string = hrefs[i];
  var countryListLinks = string.replace(pattern, '');
  console.log(countryListLinks);
}
}
if (doSelect('Location').length > 0 && doSelect('Date').length > 0) {
  countryList();
};