在书签中使用 jQuery UI
在 CoffeeScript 中,尽管此代码几乎与 JavaScript 相同:
tabs_html = "<div id='nm-container'><ul><li><a href='#tabs-1'>Guidelines</a></li><li><a href='#tabs-2'>Test</a></li></ul>
<div id='tabs-1'><p>something1</p></div><div id='tabs-2'><p>something2</p></div></div>"
$("#nm-toolbar").append(tabs_html)
$("#nm-container").tabs()
但它不起作用。有趣的是,当从控制台尝试最后一行: $("#nm-container").tabs()
时,它确实有效。我在下面附上完整的代码。请注意,我使用 CoffeeMarklet 生成书签,该书签似乎仅适用于 chrome。
s1 = window.document.createElement('script')
s1.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'
window.document.body.appendChild(s1)
$ ->
s2 = window.document.createElement('script')
s2.src = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js'
window.document.body.appendChild(s2)
jqueryUIcss = window.document.createElement('link')
jqueryUIcss.rel = 'stylesheet'
jqueryUIcss.type = 'text/css'
jqueryUIcss.href = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/blitzer/jquery-ui.css'
window.document.head.appendChild(jqueryUIcss)
if $("#nm-toolbar").length == 0
toolbar = "<div id='nm-toolbar'></div>"
$("body").append(toolbar)
$("#nm-toolbar").css({
'background': '#fafafa',
'height': '500px',
'width': '400px',
'position': 'fixed',
'bottom': '0px',
'right': '20px',
'padding': '5px'
})
tabs_html = "<div id='nm-container'><ul><li><a href='#tabs-1'>Guidelines</a></li><li><a href='#tabs-2'>Test</a></li></ul>
<div id='tabs-1'><p>something1</p></div><div id='tabs-2'><p>something2</p></div></div>"
$("#nm-toolbar").append(tabs_html)
$("#nm-container").tabs()
In CoffeeScript, though this code is almost identical to JavaScript:
tabs_html = "<div id='nm-container'><ul><li><a href='#tabs-1'>Guidelines</a></li><li><a href='#tabs-2'>Test</a></li></ul>
<div id='tabs-1'><p>something1</p></div><div id='tabs-2'><p>something2</p></div></div>"
$("#nm-toolbar").append(tabs_html)
$("#nm-container").tabs()
It doesn't work. Funny thing is it does work when trying the last line: $("#nm-container").tabs()
from the console. I'm attaching the full code below. Note that I'm using CoffeeMarklet to generate the bookmarklet which seems to work only on chrome.
s1 = window.document.createElement('script')
s1.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'
window.document.body.appendChild(s1)
$ ->
s2 = window.document.createElement('script')
s2.src = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js'
window.document.body.appendChild(s2)
jqueryUIcss = window.document.createElement('link')
jqueryUIcss.rel = 'stylesheet'
jqueryUIcss.type = 'text/css'
jqueryUIcss.href = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/blitzer/jquery-ui.css'
window.document.head.appendChild(jqueryUIcss)
if $("#nm-toolbar").length == 0
toolbar = "<div id='nm-toolbar'></div>"
$("body").append(toolbar)
$("#nm-toolbar").css({
'background': '#fafafa',
'height': '500px',
'width': '400px',
'position': 'fixed',
'bottom': '0px',
'right': '20px',
'padding': '5px'
})
tabs_html = "<div id='nm-container'><ul><li><a href='#tabs-1'>Guidelines</a></li><li><a href='#tabs-2'>Test</a></li></ul>
<div id='tabs-1'><p>something1</p></div><div id='tabs-2'><p>something2</p></div></div>"
$("#nm-toolbar").append(tabs_html)
$("#nm-container").tabs()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑问题是您正在异步加载 jQuery UI。该行
开始加载 jQuery UI,但您的代码在 jQuery UI 必须加载之前继续。这可以解释为什么代码中的
tabs()
调用失败,但是当您在脚本有时间加载后从控制台执行此操作时,它会成功。您应该能够通过使其余代码从回调
Edit 运行来解决此问题:就此而言,您确实应该对
s1
执行相同的操作,或者否则$ ->
调用可能会失败。它成功的事实表明您的浏览器中已经缓存了 jQuery,或者页面上已经有 jQuery。您还应该使用noConflict
来避免覆盖页面的现有 jQuery 版本。 Acorn 链接到的 运行 jQuery 代码书签执行所有这些操作(并且以比此答案中的代码更跨浏览器的方式)。I suspect that the problem is that you're loading jQuery UI asynchronously. The line
starts loading jQuery UI, but your code continues before jQuery UI has necessarily been loaded. That would explain why the
tabs()
call in your code fails, but it succeeds when you do it from the console, after the script has had time to load.You should be able to fix this by making the rest of your code run from the callback
Edit: And for that matter, you really should do the same thing with
s1
, or else the$ ->
call could fail. The fact that it's succeeding suggests that either you have jQuery cached in your browser, or the page already has jQuery on it. You should also usenoConflict
to avoid overwriting the page's existing jQuery version. The Run jQuery Code Bookmarklet that Acorn linked to does all of these things (and in a more cross-browser manner than the code in this answer).这应该有效:
如果使用上述代码,您需要取消选中 CoffeeMarklet 页面上的“添加 jQuery”选项
更新:
添加了对 jQuery 和 jQuery UI 是否存在的检查,因此不会不必要地加载它。
尽管可以通过检查是否已经存在正确的 jQuery 版本(如 Ben Almans 代码那样)来改进它。归因:
Beygi 给出了 一个用于加载 javascript 的可爱片段资源纷纷涌现。
This should work:
You'd want to uncheck the "add jQuery" option on the CoffeeMarklet page to if using the above code
Update:
Added checking for presence of jQuery and jQuery UI so it isn't loaded unnecessarily.
Although it could be improved by checking to see if the correct version of jQuery is already present as Ben Almans code does.Attribution:
Beygi gave a lovely snippet for loading javascript resources one after another.