我試圖使用這裏提供的解決方案Meta viewport ONLY for phones?只在手機上生成一個元視口標記,我將該腳本添加到文檔的頭部分,但它不會被生成,並且我得到一個「語法錯誤:。預期令牌‘)’的錯誤控制檯上元視口標記腳本不工作
下面是我使用的腳本:
<script type="text/javascript">
$(document).ready(function(){
if(navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)) {
document.write('<meta name='viewport' content='width=device-width, user-scalable=no'>');
}
});
</script>
現在它刪除整個文件的HTML ...在加載頁面的一瞬間內容閃爍秒鐘,然後消失,我得到的是一個空白頁面(在Web檢查器中實際的HTML是空的) –
而不是'document.write',我建議使用更可靠的方法,如在這個SO問題中給出的:http://stackoverflow.com/questions/3588628/can-i-change-the-viewport-meta-tag-in-mobile-safari-on-the-fly – Ryan
完美的是,使用'('head')。append'而不是'document.write'完成了這個訣竅。 –