在Chrome,safari和opera中將背景圖像設置爲絕對引用,例如:「/images/image.png」將其更改爲「http://sitepath/images/image.png」。用javascript設置背景圖像
它不會在Firefox中這樣做。
有什麼辦法可以避免這種行爲,或者是否寫入瀏覽器的javascript引擎?使用jquery設置背景圖像也會造成這個問題。
問題是,我張貼的HTML腳本需要在這種特定格式的URL的PHP腳本。我知道設置圖像路徑相對修復了這個問題,但我無法做到這一點。
唯一的另一種選擇是使用正則表達式。轉換網址。
謝謝。
測試此在Firefox和Chrome/WebKit瀏覽器:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div style="height:400px;width:400px;background-image:url(/images/images/logo.gif);">
</div>
<br />
<br />
<div id="test" style="height:400px;width:400px;">
</div>
<script type="text/javascript" src="/javascripts/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#test").css('background-image',"url(/images/images/logo.gif)");
alert(document.getElementById('test').style.backgroundImage);
});
</script>
</body>
</html>
就個人而言,我不會試圖依靠首先是這樣的。我嘗試了4個瀏覽器,並且他們都給出了稍微不同的結果。 – goat 2010-05-01 16:46:18