帮助在UIWebView中使用Javascript代码 [英] Help Using Javascript Code in a UIWebView

查看:114
本文介绍了帮助在UIWebView中使用Javascript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在webView中运行以下代码,以便内容可编辑:

I would like to run the following code in a webView, so that the content is editable:

javascript: document.body.contentEditable ='true'; document.designMode='on'; void 0

到目前为止我已经尝试过:

So far I have got as far as trying this:

- (void)webViewDidFinishLoad:(UIWebView *)webViews  
{    
    NSString *string = @"javascript: document.body.contentEditable ='true'; document.designMode='on'; void 0";
    [webView stringByEvaluatingJavaScriptFromString:string];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:string]]];
    NSLog(@"finished load");
}

如果你能告诉我如何获得这个工作将是巨大的。 / p>

If you could tell me how to get this working that would be great.

推荐答案

我喜欢在JavaScript中封装我的JavaScript调用。我的意思是,如果有多行要在JavaScript中运行,将其包装在函数中,并只调用函数。这个分离的逻辑出来干净,让你测试的东西严格在Safari中,而不建立一个iPhone应用程序围绕它。

I like wrapping up my JavaScript calls in JavaScript. What I mean by this is, if there are multiple lines to be run in JavaScript, to wrap that in a function and call just the function. This separates the logic out cleanly and let's you test things strictly in Safari without building an iPhone app around it.

此外,我同意Evan说关于摆脱 [webView loadRequest:

Also, I agree with what Evan said about getting rid of the [webView loadRequest:.

编辑:通过结束,我的意思是:

By wrapping up, I mean something like:

function foo() { document.body.contentEditable ='true'; document.designMode='on'; }

然后在iOS的调用中调用foo。

And then just calling "foo" in your call from iOS.

这篇关于帮助在UIWebView中使用Javascript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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