PHP 中的 DOM 类注入
通过jQuery
,我能够使用类 first 标记文档中的所有
我稍后可以对其进行样式设置(即 :first-child
和 :last-child
元素(好吧,几乎所有:))ul#navigation
中的第一个 li 可以轻松地寻址为 ul#navigation .first
)。
我使用了以下代码:
var $f = $('*:first-child')
$f.addClass('first');
var $l = $('body *:last-child')
$l.addClass('last');
示例
示例已经在这里 -然而,这不是这样做的方法,这只是一个以其他语言为原型的想法,目前对我来说更简单的语言。
问题
现在,我的问题是是否可以通过 php
执行相同的操作,因此非 JS 用户/小工具可以具有相同的效果和额外的样式,并且在浏览器上也不会那么矫枉过正。
那么,是否可以捕获输出,将其解析为 html 并在 php
中轻松注入此类?
澄清
我很清楚输出缓冲
,只是还没有用它做太多事情 - 另外,我不确定如何修改php中的输出
作为解析的dom(没有string
正则表达式
) - 以及在服务器上的难度 - 当然有缓存,所以这整个东西将运行一次,直到页面再次被编辑。
idea
Via jQuery, I was able to mark all :first-child
and :last-child
elements in document (well, almost all :)) with class first
which could I later style (i.e. first li in ul#navigation
would be easily adressable as ul#navigation .first
).
I used following code:
var $f = $('*:first-child')
$f.addClass('first');
var $l = $('body *:last-child')
$l.addClass('last');
example
Example is already here - it's not the way to do it however, it's just an idea prototyped in other, for me at the moment easier language.
question
Now, my question is if it's possible to do the same via php
, so non-JS users/gadgets could have the same effects and additional styling and also it would be less overkill on browser.
So, is it possible to capture output, parse it as html and inject this class easily in php
?
clarification
I'm quite aware of output buffering
, just haven't done much stuff with it - also, i'm not sure about modificating output string
in php
as parsed dom (without regex
) - and how tough on server it'll be - with caching of course, so this whole stuff will run once until the page will be edited again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我确信您可以使用 输出缓冲 来捕获您的组装PHP页面,然后使用 DOM 和 XPath 到 添加类属性,但问题是,为什么不首先在组装页面时将类放在元素上?为您节省 jQuery 和捕获。
另外,使用 jQuery 添加 CSS 类来执行
ul#navigation.first
也有点奇怪,因为您使用的 jQuery 表达式是一个 CSS 选择器,因此您可以直接使用它来设置样式CSS 文件中的第一个子元素。添加类.first
的唯一原因是如果您希望向后兼容无法处理:first-child
的浏览器。I'm sure you could use output buffering to capture your assembled PHP page and then use DOM and XPath on it to add the class attributes, but the question is, why don't you just put the classes onto the elements when assembling the page in the first place? Saves you the jQuery and the capturing.
Also, adding the CSS classes with jQuery to be able to do
ul#navigation.first
is somewhat odd too, because the jQuery expression you used is a CSS selector, so you could use it directly to style the first child from your CSS file. The only reason to add a class.first
is if you want to be backwards compatible with browsers unable to process:first-child
.我认为您会发现在 jQuery 中执行此操作比在 PHP 中更容易,但它也可以在 PHP 中完成。
要捕获输出,您需要输出缓冲,您可以使用 ob_start 函数,在发送任何输出之前。您可以传递
ob_start()
一个 PHP 函数,该函数将接收 HTML 代码作为参数,然后可以使用 PHP 的 DOM 函数。I think you'll find it's easier to do this in jQuery than PHP, but it can be done in PHP.
To capture output you want output buffering, which you activate with the ob_start function, before sending any output. You can pass
ob_start()
a PHP function which will receive the HTML code as a parameter and can then manipulate the HTML using PHP's DOM functions.jQuery 在客户端的浏览器中运行。 PHP 在服务器上运行。一旦服务器提供服务,您就无法在浏览器中修改 DOM。
您可以做的就是为页面提供适当的类。例如,在 PHP 中打印表格时:
ps.你真的想在没有 JS 的情况下支持客户吗?
jQuery runs in the client's borwser. PHP runs on the server. You can't modify the DOM in the browser from the server once it is served.
What you could do, is to serve the page already with the proper classes. For example in PHP when you print a table:
ps. Do you really want to support clients without JS?
您甚至可以通过使用此处<提到的一些库来使用相同的CSS选择器/a>.
我读到 phpQuery 库甚至具有您需要的相同
:first-child
pseudo-class 。我真诚地希望您计划使用缓存,否则您的CPU使用率将随着一些请求而上升100%。
You could even use the same CSS selectors by using some of the libraries mentioned here.
I read that the phpQuery library even has the same
:first-child
pseudo-class you need.I sincerely hope you plan on using caching or else your CPU usage will go up 100% with a few request.