【发布时间】:2014-09-16 19:01:37
【问题描述】:
我正在使用 PhantomJS 使用 render() 函数对一些网页进行光栅化,有时生成的图像具有黑色背景(这应该是正常行为,如 FAQ 底部所述) .
但是如果网页没有设置背景颜色,我想设置一个白色背景只有,像这样:
page.evaluate(function() {
if (getComputedStyle(document.body, null).backgroundColor === 'transparent') {
document.body.bgColor = 'white';
}
});
但它根本不起作用。我也试过:
=== 'rgba(0, 0, 0, 0)'
无济于事。
我在这里错过了什么?
【问题讨论】:
-
你试过调试吗?在您的测试中,背景颜色是什么?您可以尝试: if (!getComputedStyle(document.body, null).backgroundColor) { ... } 如果 backgroundColor 为空、未定义、假、空字符串或任何其他“假”值,这将起作用。
-
计算样式永远不会为 backgroundColor 返回 null、undefined、false 或空字符串。
-
我在 'if' 条件中插入了一条 console.log() 消息,但它从未显示。顺便说一句,我在 Firefox 和 Chrome 中使用 Javascript 控制台检查了特定网页上 'getComputedStyle(document.body, null).backgroundColor' 的值,它确实返回了 'transparent'...
-
@Silas 如果你想通知一些评论员你的评论,你应该使用@replies。
标签: javascript phantomjs