是否可以使用QueryPath选择HTML注释? [英] Is it possible to select HTML comments using QueryPath?

查看:130
本文介绍了是否可以使用QueryPath选择HTML注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这可以使用jQuery,但是如何在QueryPath中完成呢?

使用jQuery选择HTML注释

如果不是,任何人都可以提供一个可以选择注释的HTML解析器吗?

解决方案

QueryPath附带一个名为 QPXML 的扩展,它有几个附加方法。其中之一是 comment()



要使用它,只需将它包含在脚本中:

  include'QueryPath / QueryPath.php'; 
包含'QueryPath / Extensions / QPXML.php';

htmlqp($ html,$ selector) - > comment();

这将检索附加到当前选定节点的第一条评论。



如果在同一个节点中有一组非常复杂的注释,那么可以这样做:

  $ nodes = $ qp-> get(); ($ child-> nodeType == XML_COMMENT_NODE){$ b $ $ b $ $ foreach b // $孩子是一个评论。 
print $ child-> textContent;





$ p

这有点丑陋,但它可以更好地访问一个元素中有很多注释的情况。


I see this is possible using jQuery, but how can it be done in QueryPath?

Selecting HTML Comments with jQuery

If not, can anyone suggest an HTML parser that can select comments?

解决方案

QueryPath comes with an extension called QPXML that has several add-on methods. One of these is comment().

To use it, simply include it in your script:

include 'QueryPath/QueryPath.php';
include 'QueryPath/Extensions/QPXML.php';

htmlqp($html, $selector)->comment();

This will retrieve the first comment attached to the presently selected node(s).

If you have a really sophisticated set of comments all within the same nodes, you can do something like this:

$nodes = $qp->get();
foreach ($nodes as $node) {
   foreach ($node->childNodes as $child) {
      if ($child->nodeType == XML_COMMENT_NODE) {
         // $child is a comment.
         print $child->textContent;
      }
   }
}

This is a little uglier, but it gives better access to cases where one element has a lot of comments in it.

这篇关于是否可以使用QueryPath选择HTML注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆