donqo88682 2014-09-27 14:54
浏览 75
已采纳

PHP:如何使用simple_html_dom解析器将CSS text-align属性添加到元素的已存在的内联样式属性中?

I have a string variable $str which contains HTML for a part of a page. I need to add the CSS property text-align:justify to all the <p> elements in the HTML (preferably using simple_html_dom parser).

Some of the <p> elements already have an inline style attribute, others don't. So I can not do something like

$str_html = str_get_html($str);
foreach ($str->find('p') as $p) {
  $p->style='text-align:justify;';
}

Because this will void any other styles already applied to the <p> element.

PS:- I can not use JQuery. I can use JS but will only use it as a last resort.

  • 写回答

1条回答 默认 最新

  • dongmu1914 2014-09-27 15:00
    关注

    You could concatenate instead of re-assigning

    $str_html = str_get_html($str);
    foreach ($str->find('p') as $p) {
      $p->style .= '; text-align:justify;';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题