我正在处理的元素在 CSS 文件中有这样的硬编码 URL:
a.edit
{
background: url(/TestSite/Images/Edit.png) no-repeat top left;
display: inline-block;
width: 16px;
height: 16px;
padding:1px;
margin:1px;
text-indent: -9999px; /* hides the link text */
}
当站点移至生产环境时,这些链接会中断。我正在寻找一个解决方案,以便它可以在网站运行的任何地方运行。
这就是我想出的方法并且它有效,但我想知道是否有更好的方法:
<script>
$(document).ready(function () {
$("a.edit").css('background', 'url(' + $("body").data("baseurl") + 'Images/Edit.png) no-repeat top left');
});
</script>
<body data-baseurl="~/">...</body>
最佳答案
CSS 处理相对于样式表所在位置的相对 URL。利用这一点,不要在 javascript 中重写 URL。
关于jquery - CSS 文件中的硬编码 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10689430/