【问题标题】:Getting and placing content within html tag by its class using php使用 php 通过其类在 html 标记中获取和放置内容
【发布时间】:2023-03-08 07:15:01
【问题描述】:

是否可以通过类名在 html 标记中获取和放置内容? 例如:

<div class='edit'>
Wow! I'm the Content.
</div>

是否可以获取该值、编辑并将其放回或为该 div 设置新值等?如果可能的话......如果它有多个类,它会起作用吗?喜欢:

<div class='span-20 edit'>
Wow! I'm the Content.
</div>

【问题讨论】:

  • 这是已经发送到浏览器还是仍在工作中?你也可以使用 JS 来做这个客户端。

标签: php html class


【解决方案1】:

如果您可以确定要操作哪个特定的 HTML 标记,那么您可以使用各种工具。在这种情况下,您可以使用 str_replace、preg_replace、DOMDocument、DOMXPath 和 simplexml。

【讨论】:

    【解决方案2】:

    如果在 PHP 中,试试这个:

    $xhtml = simplexml_load_string("<div class='edit'>Wow! I'm the Content.</div>");
    $divs = $xhtml->xpath('//div[@class=edit]');
    if (!empty($divs){
        foreach ($divs as $div){
            $div['class'] .= ' span-20';
        }
    }
    return $xhtml->asXML();
    

    使用 jQuery javascript 库,执行以下操作:

    $('.edit').addClass('span-20');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 2012-04-13
      • 2015-03-30
      相关资源
      最近更新 更多