このコードは見ましたが、正しく実行できません。iOSのプログラムでjQueryとJavaScriptをHTMLに挿入する
- (void)viewDidLoad {
[super viewDidLoad];
_webview.delegate = self;
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"Samplehtml" ofType:@"html"]isDirectory:NO]]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *jsFile = @"jquery-1.11.0";
NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:jsFile ofType:@"js"];
NSURL *jsURL = [NSURL fileURLWithPath:jsFilePath];
NSString *javascriptCode = [NSString stringWithContentsOfFile:jsURL.path encoding:NSUTF8StringEncoding error:nil];
[_webview stringByEvaluatingJavaScriptFromString:javascriptCode];
}
これは私が試していることです。
MyJavaScript.js
$("p").click(function() {
alert("hello");
})
Samplehtml.html
<!DOCTYPE html>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
エラーが発生していますか?ファイルパスを取得しているかどうかを確認してください。 –
はい私はjsFileのパスを取得しています。私は自分のhtmlページを開いているときにこのjsがどのように動作するのか知りたい。 htmlを開くために使用しているコードは、NSString * htmlFile = [[NSBundle mainBundle] pathForResource:@ "sample" ofType:@ "html"]です。 NSString * htmlString = [NSString stringWithContentsOfFile:htmlファイルのエンコード:NSUTF8StringEncodingエラー:nil]; [webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]]; –
私はこのファイル 'jsFile'について話しています –