如何设置背景图像url没有自动前缀的主机在Chrome
How to set background-image url without auto prefixed host in Chrome?
假设index.html
的url是http://localhost/index.html
<div id='test'></div>
<script>
var el = document.querySelector('#test');
el.style.background = 'url("/test.png")';
</script>
输出html将是:
<div id="test" style="background: url(http://localhost/test.png);"></div>
我不想要主机前缀http://localhost
。如何防止浏览器的默认行为?
为什么有这个要求?我正在开发一个页面编辑器。我需要发回干净的html后的html被编辑到服务器。
我只是想知道是否有一种优雅的方式来实现它。
尝试添加基本标签…在你的例子中是
<base href="http://localhost" target="_blank">
,然后在脚本中使用:
el.style.backgroundImage = 'url("test.png")';
站立//localhost/test.png存在,和记得给div正确的尺寸!
相关文章:
- Chrome WebKitGetUserMedia
- JQueryhide()不适用于Mozzilla,但适用于Chrome
- 在chrome.tabs.onCreated之后加载HTML页面
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- chrome扩展更改主机/域警告
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- Google/html5语音识别JavaScript SDK Chrome网络工具包SpeechRecognition
- 激活chrome上的chrome.notifications对象
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- JS可以在Chrome中工作,但不能在Firefox中工作
- Chrome加载旧版本的Javascript文件
- 试图阻止Chrome通过扩展关闭
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 如何设置背景图像url没有自动前缀的主机在Chrome