繁体   English   中英

阅读HTML并从特定评论部分中选择所有href

[英]Read Html and pick all href from the specific comment section

我想从<a>读取所有href和从生成的html中读取src ,问题是hrefsrc包含在单独的注释块中,如下所示:

<!-- Start of the CTA Button1 -->

<a target="_blank" href="http://www.home.test.com/tes/redirector.jspx?action=ref&cname=test_EDITORIAL&ckey=63746&cc=US&lc=eng&mcr=true&cmpid=3455AM" style="cursor:hand;color:#ffffff;text-decoration:none">download configuration guide&nbsp;</a>

<!-- Starting of another comment -->

我想阅读特定于此注释的每个href ,以便以后知道该href是此注释部分的一部分。 有谁知道如何用jquery或javascript实现它?

我用/n分割了整个html。 我收到注释行,但不知道如何获取href因为它没有任何ID。

将您的链接放入DIV中以获得更好的表现。

var linkArray = [];
var elementsInDiv = document.getElementById('myDiv').childNodes; //get all child nodes from BODY

for (var i = 0; i < elementsInDiv.length; i++) { //Loop all childNodes
    //If child node == comment AND there is a next element...
    if (elementsInDiv[i].nodeType === 8 && elementsInDiv[i].nextElementSibling) {
        var patt = new RegExp("Start of the CTA Button1"); //...define search pattern
        //if searc pattern in comment AND the next element is an anchor...
        if (patt.test(elementsInDiv[i].textContent) && elementsInDiv[i].nextElementSibling.tagName === 'A') { 
            linkArray.push(elementsInDiv[i].nextElementSibling.href); //..append href attribute to array
        }
    }
}

console.log(linkArray); //All href attributes

的jsfiddle

尝试这个:

$("*")
.contents()
.filter(function(){ return this.nodeType == 8;})
.each(function(i,e){ 
    if(e.nodeValue.contains('Button1'))
        alert(this.nextElementSibling.href);
});

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM