簡體   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