PHP 在字符串中查找带有 id 的元素并添加样式

标签 php html css

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 4 年前

我有一个 HTML 字符串。如何找到带有标签 #test 的 div 并向其添加样式属性,例如 style="page-break-inside:avoid;"

最佳答案

简单你可以使用PHP Html parser . 例如听到:

$dom = new Dom;
$dom->load('<div id="test"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
$div = $dom->find("#test");
$div->setAttribute('style', 'page-break-inside:avoid;');
echo $dom->outerHtml;

关于PHP 在字符串中查找带有 id 的元素并添加样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54763101/

上一篇:php - 如何将 css 写入 php 代码段

下一篇:css - Google 字体显示不一致

相关文章:

php - 这段代码中如何返回数组的值?

html - Bootstrap : 1 col sidebar taking height of the content of the other cols

javascript - Bootstrap 模式切成两半

html - 居中固定/锚定页脚?

php - 我们应该在 Amazon MWS PHP SDK 的 APPLICATION_NAME 和 APPLICATION_VERSION 字段中输入什么?

php - 针对数据库问题的查询效率

php - 在 SugarCRM 中将国家/地区文本字段更改为下拉列表

javascript - 使用 jquery 禁用 html 表单中的提交按钮的功能,但它仍然可见

twitter-bootstrap - 如何使用 bootstrap 将两个图像 "horizontally"(并排)放在一起,这样它们就不会伸出屏幕?

html - 我该怎么做才能让我的 div 正确 float ?