如果你不想在客戶端使用JavaScript,那麼你最終必然會在服務器端執行它。我可以想象的是,您可以檢查訪問者使用的瀏覽器(&版本)的UserAgent,並相應地顯示SVG或PNG。也許這是一個選擇?
編輯: 好吧,看起來你還在考慮JavaScript。 就我個人而言,我會選擇始終將img-tag指向一個PNG。然後,當文檔準備就緒後,我會通過所有這些img標籤,並用指向SVG的嵌入標籤替換它們。
jQuery的例子:使用
if(Modernizr.svg) // Wohooo SVG words: let's party
{
$('img.replace').each(function() { // Loop through all PNGs that need replacing
// A unique identifier for images is saved in the id-attribute: use it to append the SVG version
$(this).append('<embed src="' + $(this).attr('id') + '.svg" type="image/svg+xml" />');
$(this).remove(); // Clean-up the unused img-tag
});
}
上面的例子中,像
<img class="replace" id="123456" src="123456.png"/>
一個img標籤將與
<embed src="123456.svg" type="image/svg+xml" />
或許這對你的作品被取代? ;-)
我讀了一大堆文章,其中大多數人都說img標籤中的svg不支持......但那些文章一定是過時的。我想我現在開始使用img標籤。 關於內容談判,我不知道這是多麼可靠... ...必須研究一個更大的。 – Housni 2012-01-11 14:39:46
如果這有幫助,你有沒有[考慮接受它作爲答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)? – adiabatic 2012-01-12 04:43:36
我用兩個答案的組合,實際上是:。 如果(Modernizr.svg){ $( 'img.svg')ATTR( 'src' 中,函數(){ 收益$(本).attr ('src')。replace('。png','.svg'); }); } 縮進了4位顯示格式的代碼似乎並沒有爲我工作:/ – Housni 2012-01-12 11:34:44