CSS 不适用于小书签
我正在创建一个简单的书签(以前从未这样做过),虽然脚本执行得很好(现在没有做太多事情),但嵌入的 CSS 样式表不适用于 HTML。
激活链接如下(域已更改):
<a href="javascript:(function(){document.head.appendChild(document.createElement('script')).src='http://www.mydomain.co.uk/cloudlr/app/js/cloudlr.js?rand=' + Math.floor(Math.random()*99999);})();">Do this</a>
然后调用的脚本是:
if (typeof jQuery == 'undefined') {
appendjQuery();
} else {
jsVersion = $().jquery;
versionArray = jsVersion.split('.');
if (versionArray[1] < 6) {
appendjQuery();
} else {
runthis();
}
}
function appendCSS() {
var cloudlrCSS = document.createElement('link');
cloudlrCSS.type = 'text/css';
cloudlrCSS.href = 'http://www.mydomain.co.uk/cloudlr/app/css/screen.css';
cloudlrCSS.media = 'all';
document.head.appendChild(cloudlrCSS);
}
function appendjQuery() {
var cloudlrJS = document.createElement('script');
cloudlrJS.type = 'text/javascript';
cloudlrJS.onload = runthis;
cloudlrJS.onreadystatechange = function () {
if (this.readyState == 'loaded' || this.readyState == 'complete') {
runthis();
}
}
cloudlrJS.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js';
document.head.appendChild(cloudlrJS);
}
function runthis() {
appendCSS();
$(document).ready(function() {
$("img").each(function() {
var $this = $(this);
var imageWidth = $(this).width();
$(this).before(imageWidth);
});
var cloudlrOverlay = '<div id="cloudlr-content">This is some content.</div><div id="cloudlr-overlay"></div>';
$("body").append(cloudlrOverlay);
});
}
希望这一切都非常简单。
脚本执行得很好(jQuery),但没有应用 CSS。如果我查看“生成的源”,我可以看到 CSS 插入得很好并且文件的链接有效。所以我不确定发生了什么事。
我对此很陌生,欢迎任何有关最佳实践以及问题解决方案的其他建议。
非常感谢, 迈克尔.
I'm creating a simple bookmarklet (never done this before) and while the script executes just fine (doesn't do much right now), the CSS style sheet that gets embed doesn't apply to the HTML.
The activation link is as follows (domain changed):
<a href="javascript:(function(){document.head.appendChild(document.createElement('script')).src='http://www.mydomain.co.uk/cloudlr/app/js/cloudlr.js?rand=' + Math.floor(Math.random()*99999);})();">Do this</a>
Then the script called is:
if (typeof jQuery == 'undefined') {
appendjQuery();
} else {
jsVersion = $().jquery;
versionArray = jsVersion.split('.');
if (versionArray[1] < 6) {
appendjQuery();
} else {
runthis();
}
}
function appendCSS() {
var cloudlrCSS = document.createElement('link');
cloudlrCSS.type = 'text/css';
cloudlrCSS.href = 'http://www.mydomain.co.uk/cloudlr/app/css/screen.css';
cloudlrCSS.media = 'all';
document.head.appendChild(cloudlrCSS);
}
function appendjQuery() {
var cloudlrJS = document.createElement('script');
cloudlrJS.type = 'text/javascript';
cloudlrJS.onload = runthis;
cloudlrJS.onreadystatechange = function () {
if (this.readyState == 'loaded' || this.readyState == 'complete') {
runthis();
}
}
cloudlrJS.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js';
document.head.appendChild(cloudlrJS);
}
function runthis() {
appendCSS();
$(document).ready(function() {
$("img").each(function() {
var $this = $(this);
var imageWidth = $(this).width();
$(this).before(imageWidth);
});
var cloudlrOverlay = '<div id="cloudlr-content">This is some content.</div><div id="cloudlr-overlay"></div>';
$("body").append(cloudlrOverlay);
});
}
Hopefully that's all pretty straightforward.
The scripts execute just fine (jQuery) but no CSS is applied. If I view the "generated source" I can see that the CSS was inserted fine and the link to the file works. So I'm not sure what's going on.
I'm new to this and would welcome any additional recommendations on best practices as well as a solution to the problem.
Many thanks,
Michael.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个常见的错误......我认为这可以帮助你
this is a common mistake ... I think this could help you