首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery:解析HTML

Jquery:解析HTML
EN

Stack Overflow用户
提问于 2015-11-16 09:36:50
回答 1查看 98关注 0票数 1

我需要解析href标记的帮助。目前,所有内容都被解析为文本,但是我需要解析链接,以便以后可以使用AJAX将其发送到php页面。

我的HTML看起来像:

代码语言:javascript
复制
<div id="word_content">
<br>Testing Time: 2015-10-29 17:57:11<br>
    Total Age: 19<br>
    Total Friemd: 9<br>
    Total Family: 10<br>
    <br>
Here are the suggestions  - Him_530037_: <a href="www.mytarget.com="_blank">93358546</a>
<h3>Overview</h3><br>
<ul>
    <li>(The overlap provided is not good)</li>
</ul>

<h3>Structure</h3><br>
<h4>Target:</h4><br>
<ul>
    <li>Audience.</li>
    <li>Lookalike</li>
    <li>Overlap of Audience</li> 
    <a href="https://www.myPage.com/lolPagess/?id=06" target="_blank">06<font name="names" hidden="" style="display: inline;"> - Page Likes</font></a>           
</ul>

Jquery如下所示:

代码语言:javascript
复制
var headTags = $("div#word_content").find("*").filter(function(){
                return /^h/i.test(this.nodeName);
              });

              var output = {};

              $(headTags).each(function(){
                var currentHead = $(this);

                var nextNextElem = currentHead.next().next();
                var innerText = [];
                if(nextNextElem.prop("tagName") == "UL")
                  {
                     nextNextElem.find("li").each(function(){
                       innerText.push($(this).text());
                     });  

                  }

                output[currentHead.text()] = innerText;
              });  

目前,Jquery正在获取数据,但它只捕获文本而不是链接。我也需要解析这个链接,这样这个链接就可以在更多的页面中使用。有人能帮忙吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-16 09:46:47

用这个:

代码语言:javascript
复制
 nextNextElem.find("a").each(function(){
         innerText.push($(this).text()+" & href is:"+$(this).attr("href"));                   
                         }); 

代码语言:javascript
复制
var headTags = $("div#word_content").find("*").filter(function(){ 
	return /^h/i.test(this.nodeName); 
	}); 

	var output = {}; 

	$(headTags).each(function(){ 
	var currentHead = $(this); 

	var nextNextElem = currentHead.next().next(); 
	var innerText1 = []; 
	if(nextNextElem.prop("tagName") == "UL") 
	{ 
	nextNextElem.find("li").each(function(index){ 
	innerText1.push(this.firstChild.data);
	$(this).children().each(function(index){ 
	innerText1.push("<a href='"+$(this).attr("href")+"'>"+$(this)[0].innerText+"</a>"); 
    if($(this).prop('nextSibling')){
	   innerText1.push($(this).prop('nextSibling').nodeValue);
         }
	}); 
	}); 

	} 

	output[currentHead.text()] = innerText1; 
	});      console.log(output);
             $("#data").html(JSON.stringify(output));
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <div id="word_content">
<br>Testing Time: 2015-10-29 17:57:11<br>
Total Age: 19<br>
Total Friemd: 9<br>
Total Family: 10<br>
<br>
Here are the suggestions  - Him_530037_: <a href="www.mytarget.com="_blank">93358546</a>
<h3>Overview</h3><br>
<ul> 
<li>Multiple Countries 
<a href="https://www.myTarget.com/ads/?id=603" target="_blank">603<font name="names" hidden="" style="display: none;"> - Post: "သင့္ရဲ့ Data အသံုးျပဳ မွုကို အေၾကာင္းၾကားေပးေသာ..."</font></a> (MM, SG), 
<a href="https://www.myTarget.com/ads/?id=602" target="_blank">602<font name="names" hidden="" style="display: none;"> - Post: "Mynamar pics."</font></a></li> 

</ul>
</div>
<span>OUTPUT AREA:</span>
<div id="data"></div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33732182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档