jQuery 和 Windows 8 JavaScript Metro 风格应用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7420356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery and Windows 8 JavaScript Metro Style Apps
提问by pfeilbr
Can jQuery be used in Windows 8 Metro-style apps developed using the JavaScript API? I'm looking at the samples, and there's a lot of standard DOM manipulation like document.getElementById
, addEventListener
, etc. I'd like to use jQuery for productivity.
jQuery 能否用于使用 JavaScript API 开发的 Windows 8 Metro 风格应用程序?我在看样品,并且有很多的标准DOM操作一样的document.getElementById
,addEventListener
等我想使用jQuery生产力。
回答by Andrew
The $
is not magic. jQuery is just a javascript library. It should run fine.
这$
不是魔法。jQuery 只是一个 javascript 库。它应该运行良好。
The only thing that might not work are cssHooks which won't take into account any 'quirks' of their environment.
唯一可能不起作用的是 cssHooks,它不会考虑其环境的任何“怪癖”。
So yes, it will work, ifyou trust Microsoft to produce an environment that follows the HTML/CSS/JS specifications.
所以是的,如果您相信 Microsoft 会生成遵循 HTML/CSS/JS 规范的环境,那么它会起作用。
Do you feel lucky?
你觉得幸运吗?
回答by Adam Roderick
Yes, it works, as will any JavaScript library. I verified it on one of the tablets they gave out at the BUILD conference.
是的,它可以工作,就像任何 JavaScript 库一样。我在他们在 BUILD 会议上分发的其中一款平板电脑上进行了验证。
However, do not expect to use a CDN to load in the script. You have to download it, add it to the project, and reference it locally.
但是,不要期望使用 CDN 加载脚本。您必须下载它,将其添加到项目中,然后在本地引用。
回答by Sherbrow
tl;dr: you can use jQuery 1.7+, but it's not (yet?) fully integrated when markup modification is involved.
tl;dr:您可以使用 jQuery 1.7+,但在涉及标记修改时,它还没有(还?)完全集成。
Quote from the Dev Center (msdn)
来自开发中心 (msdn) 的报价
Using jQuery
You can use jQuery in your Windows Store app using JavaScript, but only versions 1.7 and later. We recommend always using the latest version.
使用 jQuery
您可以使用 JavaScript 在 Windows 应用商店应用程序中使用 jQuery,但仅限 1.7 及更高版本。我们建议始终使用最新版本。
That means that it will work, but you will encounter some non-standard behaviors as listed in the HTML and DOM API changes list (msdn)
这意味着它可以工作,但您会遇到一些非标准行为,如HTML 和 DOM API 更改列表 (msdn) 中所列
There are exceptions mentioned when setting the innerHtml
property (among others : Making HTML safer) if there is unsafe markup, but this is non-blocking and the simple fact of loading the jQuery library (1.8.2) will trigger a few.
如果存在不安全标记,则在设置innerHtml
属性时会提到一些例外情况(其中包括:使 HTML 更安全),但这是非阻塞的,加载 jQuery 库 (1.8.2) 的简单事实会触发一些例外情况。
As mentioned before, you do need to use a local copy of the library (no CDN).
如前所述,您确实需要使用库的本地副本(无 CDN)。
回答by Glenn Ferrie
You can use any JS library. I suspect that methods like WinJS.xhr (aka '$.ajax') where written so that WinJS doesnt have a dependency on jQuery.
您可以使用任何 JS 库。我怀疑像 WinJS.xhr(又名“$.ajax”)这样的方法在编写时是为了使 WinJS 不依赖于 jQuery。
回答by Peter C
JQuery should work fine but bear in mind there is some built in functionality for doing this kind of thing, in the case of your example:
JQuery 应该可以正常工作,但请记住,在您的示例中,有一些内置功能可以执行此类操作:
WinJS.Utilities.query("#someId li")
.listen("click", function (result) {
回答by Serg
Don't use any windows libraries like WinJS. Why would you want to lock yourself and be dependent on proprietary code? Stick with the usual stuff, i.e. HTML, CSS, JS(JQuery or otherwise). It will only make your future support/dev far more predictable ...
不要使用任何像 WinJS 这样的 Windows 库。为什么要锁定自己并依赖专有代码?坚持使用通常的东西,即 HTML、CSS、JS(JQuery 或其他)。它只会使您未来的支持/开发更加可预测......
回答by David Isbitski
As with all third party JavaScript libraries there are a couple of things you should keep in mind.
与所有第三方 JavaScript 库一样,您应该记住几件事情。
- Always try to include the .js sourcefile in your project when possible. Calling out to a CDN or external hosted location will flag you for some security checks you wouldn't normally have to go through.
- I have used a couple of jQuery versions in coding my own apps. Some of them broke so keep in mind there is still no guarantee. Other libraries, like SoundJS, I used older versions that I know do work.
- If you are looking for some examples of third party libraries I have some posts on Knockout and Upshot here:
- http://blogs.msdn.com/b/davedev/archive/2012/05/02/connecting-to-wcf-ria-services-in-a-windows-8-metro-style-app-using-upshot-js-and-kockout-js.aspx
- 如果可能,请始终尝试在您的项目中包含 .js 源文件。呼叫 CDN 或外部托管位置将标记您进行一些通常不需要通过的安全检查。
- 我在编写自己的应用程序时使用了几个 jQuery 版本。其中一些坏了,所以请记住,仍然无法保证。其他库,比如 SoundJS,我使用了我知道可以工作的旧版本。
- 如果您正在寻找第三方库的一些示例,我在这里有一些关于 Knockout 和 Upshot 的帖子:
- http://blogs.msdn.com/b/davedev/archive/2012/05/02/connecting-to-wcf-ria-services-in-a-windows-8-metro-style-app-using-upshot- js-和-kockout-js.aspx
I have also written a free Windows Store Gaming Kit that uses third party libraries you can grab the free source for that here: http://win8gamekit.codeplex.com
我还编写了一个免费的 Windows Store Gaming Kit,它使用第三方库,您可以在此处获取免费源:http: //win8gamekit.codeplex.com
回答by Marco
It should run fine. jQuery is just a library. The $('#someid')
will internally call the document.getElementById
You can use it just as if you can use the javascript code you write yourself...
它应该运行良好。jQuery 只是一个库。该$('#someid')
会内部调用document.getElementById
,您可以使用它,就好像你可以使用你自己编写的JavaScript代码...
回答by Eric Leroy
Actually you can download it through Visual Studio. If you download NuGet Package Manager.
TOOLS->Add Packages & Extensions
其实你可以通过Visual Studio下载它。如果您下载 NuGet 包管理器。
工具->添加包和扩展
Search the left online packages for NuGet Package Manager and install.
在左侧的在线包中搜索 NuGet 包管理器并安装。
Then when in a project that you want jQuery, right click on your references and add a NuGet package, and search jQuery. That's what I use.
然后当在一个你想要 jQuery 的项目中时,右键单击你的引用并添加一个 NuGet 包,然后搜索 jQuery。我就是这么用的。