JS-文档.撰写问题



我正在尝试使用网站的document.write选项来设置每隔一个网站都会出现的重复标题。不幸的是,我遇到了一个错误,因为我无法设置背景图像,因为它会给我一个解析错误。

document.write('<div style="background-image: url('images/index/1ApBer2016.JPG'); background-repeat: no-repeat; background-size: cover;" class="header"><h1>XXX<br> XXX</h1>
</div>'
);

我想这是因为图像本身有引号,但我似乎无法修复它。你们有什么帮助吗?

使用转义符"">

document.write('<div style="background-image: url('images/index/1ApBer2016.JPG'); background-repeat: no-repeat; background-size: cover;" class="header"><h1>XXX<br> XXX</h1></div>');

使用ES6后台信息`。您不再需要空格中的任何字符和代码看起来也干净可读。

document.write(`<div style="background-image: url('https://picsum.photos/200/300'); background-repeat: no-repeat; background-size: cover;" class="header"><h1>XXX<br> XXX</h1>
</div>`);

演示:

document.write(`<div style="background-image: url('https://picsum.photos/200/300'); background-repeat: no-repeat; background-size: cover;" class="header"><h1>XXX<br> XXX</h1>
</div>`);

最新更新