我在 jsFiddle 中需要将 viewport metatag 放在 head
元素中。但由于 jsFiddle 已经包含了html、head和body标签,它会显示一个警告:“不需要HTML标签,输出中已经有了。”
有没有一种方法可以将 viewport metatag 放在 head 中?
<meta name="viewport" content="width=device-width, initial-scale=1" />
编辑 jsFiddle 的 head
标签的一种方法是使用CSS 面板 style
hack。
如果需要编辑标题,可以关闭
style
元素并访问标题。完成所有修改后,请再次打开style
标签。
/* your custom CSS */
</style>
<!-- access to the HEAD element -->
<style>
将上述代码插入CSS面板中将更改标签下的CSS部分为
<style type='text/css'>
/* your custom CSS */
</style>
<!-- access to the HEAD element -->
<style>
</style>
或者,如果您比较灵活,并且可以接受在页面加载后编辑视口,可以使用JavaScript或jQuery。
var viewport = document.createElement("meta");
viewport.setAttribute('name', 'viewport');
viewport.setAttribute('content', 'width=device-width, initial-scale=1');
document.getElementsByTagName('head')[0].appendChild(viewport);
$('head').append('<meta name="viewport" content="width=device-width, initial-scale=1" />');
http://jsfiddle.net/andreasbossard/9c3gS/
这是您参考的CSS代码:<style type='text/css'>
/* your custom CSS \*/
</style>
<!-- access to the HEAD element -->
<meta name='viewport' content='initial-scale=1.0, width=device-width' />
<style>
</style>
<?php
if(isset($_GET['url'])){
$url = $_GET['url'].'show/light/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
$content = curl_exec($ch);
curl_close($ch);
$pattern = "/<head>([^;]*); charset=UTF-8\">/";
$replacement = "<head>\n<meta charset='utf-8'>\n<meta http-equiv='X-UA-Compatible' content='IE=edge'>\n<meta name='viewport' content='width=device-width, initial-scale=1'>";
echo preg_replace($pattern, $replacement, $content);
exit;
}
else{
echo 'You must provide a jsFiddle url via the get parameter "url" like: ?url=http://jsfiddle.net/abcdef';
exit;
}
要使用此脚本,只需将其托管在某个在线位置,并使用您的jsFiddle网址作为url
获取参数调用它,如:
正如其他人所述(但在评论中被隐藏),没有一个答案能够解决问题。
jsfiddle嵌入将您的代码放置在iframe中。只有当它在最外层页面上时,viewport meta标签才会应用。请参阅我在jsfiddle上提交的此错误报告以获取示例:https://github.com/jsfiddle/jsfiddle-issues/issues/1094。
实际上只有一种解决方案,就是自己托管文件。如果您有一个发布html文件的地方,那么您就不需要fiddle了。如果您真的想麻烦,您只需要将头部添加到自己的页面中,然后将jsfiddle嵌入其中。
然而,如果您只想在自己的桌面上查看移动设备的渲染效果而不发布文件,则可以执行以下操作:
<meta name="viewport" content="width=device-width, initial-scale=1" />
<head>
等内容。 - user56reinstatemonica8