【问题标题】:Jquery: Parsing HTMLJquery:解析 HTML
【发布时间】:2015-11-16 09:36:50
【问题描述】:

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

我的 HTML 看起来像:

<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 代码是这样的:

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 正在获取数据,但它只捕获文本而不是链接。我还需要解析链接,以便可以在更多页面中使用此链接。有人可以帮忙吗?

【问题讨论】:

  • 这里nextNextElem.find("li").each(function(){ innerText.push($(this).text()); }); 你只在寻找li 但链接不是li 的一部分,所以它被排除在外了。
  • 如何添加该链接?你能帮忙吗

标签: javascript php jquery html


【解决方案1】:

使用这个:

 nextNextElem.find("a").each(function(){
         innerText.push($(this).text()+" & href is:"+$(this).attr("href"));                   
                         }); 

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));
<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>

【讨论】:

  • 让我测试一下.. 会回复你的
  • 恐怕没有生成链接。它仍在使用内部文本。
  • @user4943236 你的意思是你想把href作为链接?如果是这样的话,试试更新一个。
  • 实际上,我需要文本和 href。即我需要解析“myPage.com/lolPagess/?id=06”,这也应该成为 href 和“06”
  • 实际上,页面上有一些链接以及一些文本..所以,我需要解析所有链接和文本,并将其发送到另一个 php 文件,我将在其中提取以将它们显示为链接。目前,未解析 href,仅解析内部文本
【解决方案2】:

您可以使用类似的东西来解析站点中的链接:

$("a").each(function(i, o) {
    console.log("Link: " + (i + 1));
    console.log("  Text is: " + $(o).text());
    console.log("  Link is: " + $(o).attr('href'));
})

结果:

www.mytarget.com=
https://www.myPage.com/lolPagess/?id=06

See JsFiddle

【讨论】:

  • 我需要解析整个文本以及 href 标签
【解决方案3】:

检查a 中的每个href

$("a").each(function () {
    isUrlValid($(this).attr("href"));
});

借自Validating url with jQuery without the validate-plugin?:

  function isUrlValid(url) {
        return /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url);
    }

此正则表达式将测试有效的网址。

【讨论】:

    猜你喜欢
    • 2011-11-15
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多