jQuery选择器无法在Windows Phone 7上运行 [英] jQuery selector not working on Windows Phone 7

查看:126
本文介绍了jQuery选择器无法在Windows Phone 7上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对WP7(7.1)的网络应用程序有一个奇怪的问题。

I have a strange problem with a web app for WP7 (7.1).

适用于Chrome,Safari等的代码:

The code that works with Chrome, Safari, etc.:

$.each(val, function(sub_key, sub_val) {

                $("."+sub_key).html(sub_val);                       

});

使用中的变量示例:

sub_key : title-home
sub_val : This is home

这不适用于WP7(v.7.1)。我尝试了无穷无尽的组合,但让选择器工作的唯一方法是在选择器内定义一个静态字符串。像这样:

This does not work on WP7 (v. 7.1). I tried endless combinations but the only way to get the selector working is to define a static string inside the selector. Like so:

$.each(val, function(sub_key, sub_val) {

                $(".title-home").html(sub_val);                     

});

这当然不是一种选择。
在我加入字符串时,IE中是否存在某种错误?

This is of course not an option. Is there some kind of bug in IE when joining strings like I did?

BTW我认为WP7正在使用IE 7引擎。

BTW I think WP7 is using the IE 7 engine.

Cheers Johe

Cheers Johe

推荐答案

- 编辑:
如果您在页面上没有运行的javascripts有问题:请务必检查我对该主题的调查结果我发布了 http: //social.msdn.microsoft.com/Forums/en-AU/jscript/thread/bac9f056-e0de-449e-a1b6-36e745fa59c6 - 也许您的问题与一些不幸的时间或属性设置器排序有关。事实证明,如果您在XAML中对URL进行硬编码,那么您必须确保在该XAML中,在SOURCE属性之前设置IsScriptEnabled,否则您将有很多失败取决于您的页面具有哪种doctype。这同样适用于在代码中设置Source或Navigate',但那里很明显。另一方面,乍一看,绑定似乎无论如何都能正常工作。
- / edit

--edit: If you have problems with on-page javascripts not running at all: make sure to check my findings on the subject that I've posted at http://social.msdn.microsoft.com/Forums/en-AU/jscript/thread/bac9f056-e0de-449e-a1b6-36e745fa59c6 - maybe your problems are related to some unlucky timings or property-setter ordering. it turns out that if you hardcode the URL in XAML, then you must ensure that in that XAML the IsScriptEnabled is set BEFORE the SOURCE property, or else you will have lots of failures depedent on what doctype your pages have. the same applies to setting the Source or Navigate'ing in the code, but that's quite obvious there. on the other hand, at first glance, bindings seem to work properly either way. --/edit

它可能对你没什么帮助,但我会发布它,因为它可能会帮助很多其他人使用WP7和jQuery:

It may be not very helpful for you, but I'll post it anyways, as it may help many other people with WP7 and jQuery:

请确保您使用其团队标记的最新JQ为ok-with-wp7。我最近在与一个网站进行互操作,其中pulisher不断混合不同版本的JQ。从我简陋的实验中,只有当前的jQuery 1.6.2在WebBrowser控件中正常工作,jQueryMobile也表现不错。在我使用的那个网站上常用的1.4.x和1.5.x版本 - 完全无法解析jQ.js核心脚本的某个地方,很快就说 - 页面加载后jQuery和$符号'未定义'。 BTW。如果我没记错的话,那是7.0(来自MS:IE7和IE8之间的东西)和7.1(无意中听到某些地方:IE8和IE9之间的东西)兼容性稍微好一点,但并不完美..如果你可以先试试jQmobile。

Please, ensure that you are using the newest JQ that is marked by its team as ok-with-wp7. I recently was interoperating with a website, where the pulisher constantly mixed up different versions of the JQ. From my humble experiments, only the current jQuery 1.6.2 was working properly inside WebBrowser control, also the jQueryMobile was doing well. Versions 1.4.x and 1.5.x that was commonly used on that site I worked with - completely failed to parse somewhere at the jQ.js core script, saying shortly - jQuery and $ symbols were 'undefined' after the page loaded. BTW. If I recall correctly, that was on 7.0 (from MS: "something between IE7 and IE8") and on the 7.1 (overheard somewhere: "something between IE8 and IE9") the compatibility is a bit better, but not perfect.. If you can, try jQmobile first.

关于失败的js代码 - 扫描页面时你必须非常小心。你说WP7.1应用程序 - 所以我假设你在WebBrowser控件内工作。如果是这样 - 然后将您的JS脚本从页面中删除,将其作为字符串在您的应用程序中的某处复制,然后在WebBrowser.NavigationCompleted上 - 尝试通过WebBroweser.InvokeScript手动执行该脚本。如果有任何实际的错误 - 你将得到一个异常,很可能是ComException 0x80020101。你知道...... WebBrowser在那个东西上很奇怪,它是因为许多不同原因而返回的心爱的错误代码..无论如何:

About the failed js code -- you have to be extra-uber-careful when scanning the page. You say about WP7.1 app - so I assume you are working inside WebBrowser control. If so - then cut your JS script off the page, copy it down as a string somewhere in you app, and then, on the WebBrowser.NavigationCompleted - try to execute that script manually through WebBroweser.InvokeScript. If there's whatever actual error - you will get an exception, most probably "ComException 0x80020101. You know.. the WebBrowser is strange on that thing, it's its beloved error code returned for so many different causes.. anyways:

如果发生错误,JS会立即中止,你的代码会得到一个除了你能够捕获的内容。除了对象之外,你将得到......完全没有任何信息。可惜。但是有了一些技巧,你可以使用JS代码玩扫雷并将其降低到可以正常工作或崩溃的最小例子 - 也许你会推断出怎么了最新的。

If that error occurs, the JS is instantly aborted, and your code will get an except that you will be able to catch. Inside that except object you will get ... no information at all. Pity. But with some tricks, you can play minesweeper with the JS code and cut it down to the minimal examples that either work properly or crash - and maybe you will deduce what's up.

一些错误,例如:

- 尝试使用未知的名称,可能会返回未定义但也可能崩溃,尤其是在全局范围内< br>
- 试图意外地字符串化窗口,window.external和其他本机,可能会导致崩溃

- 尝试触摸文档的BODY,在它完全加载之前,可能会导致c皮疹

- ...
等等。请注意,在页面加载之前只是我的猜测。我假设您在收到'webbrowser.navigated'事件后尝试使用您的页面,因为这是第一个理所当然的事情,因为浏览器会通知您页面已加载。然而,即使如此,你仍然可以得到那个错误,而这可能只是关于不合时宜的时机。如果你稍后重试调用完全相同的代码,你可以成功,没有任何例外。

Some errors, for example:
- trying to use a name that is unknown, may return "undefined" but also, may crash, especially at global scope
- trying to accidentially stringify window, window.external, and other natives, may somtimes cause crash
- trying to touch the BODY of the document, before it loads completely, may cause crash
- ... and so on. Please note, that the "before the page loads" is just my guess. I assume you try to play with your page AFTER you have received the 'webbrowser.navigated' event, as it is the first sane point to do so, as the Browser notifies you the page has been loaded. Yet, still, even with that, you can still get that error, and it's may just be all about unfotunate timing. If you retry to invoke exactly the same code just a bit later, you may succeed without any exceptions.

原谅我,但我会重复:大多数时候,如果发生任何不良,您将收到未找到名称/ COM0x80020101错误,这将导致JS代码或文件以静默方式中止并被跳过。它通常不代表例外名称的含义。这只是JS中的一般错误。偶尔你会得到一些其他例外,但那是95%到5%。顺便说一句。其他例外通常也有非常无意义的数据。

Forgive me, but I'll repeat: most of the times, if anything bad occurs, you will get a "Name not found/COM0x80020101" error, that will cause the JS code or file to silently abort and be skipped. It usually does not mean what the exception name stands for. Thats just an generic-fault in the JS. Occasionally you will get a few other exceptions, but thats 95%-to-5%. Btw. other exceptions usually also have quite unmeaningful data inside them.

现在,我提到了一些技巧。

Now, I mentioned some tricks.

WebBrowser .InvokeScript是呃..让我们说,使用起来不友好。
设计来调用由javascript注册的全局函数。 InvokeScript有2个重载:(字符串名称)(字符串名称,对象[]参数)知道的重要一点是,第一个参数是函数的 NAME 。它必须是一个名字。它不能是一个任意的JS代码,InvokeScript按名称查找函数,如果找不到,则抛出一个.. 0x80020101 :)参数/字符串 - 你可以传递任何东西。

The WebBrowser.InvokeScript is erm.. let's say, unfriendly to use. It is designed only to invoke global functions registered by the javascript. The InvokeScript has 2 overloads: (string name) and (string name, object[] params). The important thing to know is, that the first parameter is the NAME of the function. It must be a name. It cannot be an arbitrary JS code, the InvokeScript looks for functions by name, and if it finds none, a .. 0x80020101 is thrown :) Arguments/strings - you may pass whatever.

我正在编写所有这些内容,以帮助您最终手动调试/扫描JS代码。如果在WebBrowser.NavigationCompleted事件处理程序中设置断点,或者之后调用的地方,您可以在调试器的监视窗口内以交互方式使用JS,使用单个,保证存在的js-core的强大技巧function: eval

I'm writing all of this to aid you with eventual manual debugging/minesweeping the JS code. If you set a breakpoint inside the WebBrowser.NavigationCompleted event handler, or wherever called afterwards, you may play with the JS quite interactively from within the debugger's watch window, with the mighty trick of a single, guaranteed-to-exist, js-core function: the eval

虽然InvokeScript只能通过eval函数调用一个名称选择的函数,但是你可以执行完全任意的代码。我(不是一次)将整个JQuery1.6.2库注入/评估到页面中,只是为了检查它是否有效或者在页面上摆弄内容 - 因为页面链接到ie。在WP7上被破坏的JQ1.4。

Although InvokeScript can invoke only a function, picked by name, with "eval" function, you may execute completely arbitrary code. I've (not once) injected/eval'ed whole JQuery1.6.2 library into the page, just to check whether it works or to fiddle with the contents on the page - because the page linked to ie. JQ1.4 that was broken on WP7.

但是,请注意,即使InvokeScript重载的声明声称返回对象,也不要被它愚弄。至少在玩eval时,他们无法这样做。他们唯一能够返回的是纯字符串。无论你试图返回什么,它都会以,空字符串的形式返回。我已经检查过7.0,7.1和7.5。对象[] params也是可疑的。我设法只传递字符串。

Be warned though, even if foth of the InvokeScript overloads claim to return an "object", do not be fooled by that. At least when playing with eval, they are unable to do so. The only thing they are able to return is a pure string. Whatever else you will try to return, it will be returned as an "", an empty string. I've checked on 7.0, 7.1 and 7.5. The object[]params is dubious too. I've managed to pass strings only.

键入Watch窗口的示例(假设'wb'是WebBrowser控件)

examples to type into the Watch window (assuming 'wb' is the WebBrowser control)

wb.InvokeScript("eval", " 'mom' "); // returns string, 'mom'
wb.InvokeScript("eval", " window "); // returns empty string
wb.InvokeScript("eval", " ' '+window "); // returns [object Object] -- note the ' '+ -- to stringify it before returning

wb.InvokeScript("window"); // 0x80020101
wb.InvokeScript(" 5+5 "); // 0x80020101 :)
wb.InvokeScript(" 'mom' "); // 0x80020101 :))
wb.InvokeScript("myfunction"); // 0x80020101 or calls your function from JS
wb.InvokeScript("function blah(){return 5+5}; ' '+blah() "); // 0x80020101

wb.InvokeScript("eval", " '5'+'5' "); // returns '55' - only with eval you may exec custom code
wb.InvokeScript("eval", "function blah(){return 5+5}; ' '+blah() "); // you can even define functions - returns 10
wb.InvokeScript("eval", " ' ' + blah() "); // what's more, whatever you do, it's permanent, functions is still there, returns 10

// JQ works too:
wb.InvokeScript("eval", " ' ' + $ "); // if you see" undefined", the JQ failed to load
wb.InvokeScript("eval", " ' ' + jQuery "); // the same as above
wb.InvokeScript("eval", myVariable_withCached_JQueryCoreCode); // works! (at least on 7.1 and JQ162)
wb.InvokeScript("eval", " ' ' + jQuery "); // jq dump, if it was loaded properly
wb.InvokeScript("eval", " ' ' + $ "); // the same
wb.InvokeScript("eval", " ' ' + $(document.body)[0] ");

在我的情况下的最后一行,有时扔,有时没有。如果它扔了,我通常能够再次成功重新调用它,例如,250ms之后,设置了一个计时器_in_the_app_code_,但是在setTimeout中无法做同样的事情!当我试图从JS内部执行类似的延迟调用时,它有时会起作用,但有时甚至会使整个应用程序崩溃。我敢打赌,例外情况是80020101,但是在花了5+之后我才对此感到生气追踪那件事情的时间..有趣的是,但是如果同一行提到document.head而不是document.body - 没有抛出任何例外。

the last line in my case, sometimes threw, and sometimes did not. If it threw, I usually was able to re-call it once again successfully for example, 250ms later, set with a timer _in_the_app_code_, but was unable to do the same in a setTimeout! When I was trying to perform a similar delay-call from within the JS, it sometimes worked, but sometimes even crashed the whole application.. I bet that the exception was 80020101, but I've just became mad about that after spending 5+ hours on tracing that thing.. Funnily, but if the same line referred to document.head instead of document.body - no exceptions thrown, ever.

我想我再次写了一点太多,抱歉:)

I think I've again written a bit too much, sorry about that :)

这篇关于jQuery选择器无法在Windows Phone 7上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆