在iOS中以编程方式将jQuery和JavaScript插入HTML

在iOS中以编程方式将jQuery和JavaScript插入HTML,javascript,jquery,html,ios,Javascript,Jquery,Html,Ios,我已经看过这段代码,但我无法正常运行它 - (void)viewDidLoad { [super viewDidLoad]; _webview.delegate = self; [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]

我已经看过这段代码,但我无法正常运行它

- (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>

我的第一个标题
我的第一段


像这样更改代码。它在我这边起作用

-(void)viewDidLoad {
    [super viewDidLoad];
    self.webView.delegate = self;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Samplehtml" ofType:@"html"];
    NSString *htmlContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:htmlContent baseURL:[NSURL URLWithString:@""]];
}
之后,在
webViewDidFinishLoad

- (void)webViewDidFinishLoad:(UIWebView *)webView {
     NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"simple" ofType:@"js"];
     NSURL *jsURL = [NSURL fileURLWithPath:jsFilePath];
     NSString *javascriptCode1 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];
     jsFilePath = [[NSBundle mainBundle] pathForResource:@"simple2" ofType:@"js"];
     jsURL = [NSURL fileURLWithPath:jsFilePath];
     NSString *javascriptCode2 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];
     [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@\n%@",javascriptCode1, javascriptCode2]];
}

希望这能对您有所帮助。

您有什么错误吗?检查是否获取文件路径?是的,我获取jsFile的路径。我想知道当我打开我的html页面时,这个js是如何工作的。我用来打开html的代码是NSString htmlFile=[[NSBundle mainBundle]pathForResource:@“sample”of type:@“html”];NSString htmlString=[NSString STRINGWITHCONTENTSOFILE:htmlFile编码:NSUTF8STRING编码错误:nil];[webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle]bundleURL]];我说的是
jsFile
这个文件是的,我得到了jsFilePath。你的项目中有
jquery-1.11.0
文件吗?你有一两个
js
文件吗?我有两个js文件。一个用于jquery,另一个用于alert。然后你需要将文件的两个内容都添加到一个字符串中。它在这里不起作用,兄弟!你能给我发你的代码吗?你的代码正在我的电子邮件中工作。我也试过你的更新代码。应用程序中仍然没有弹出警报。它在你的代码中工作吗?
- (void)webViewDidFinishLoad:(UIWebView *)webView {
     NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:@"simple" ofType:@"js"];
     NSURL *jsURL = [NSURL fileURLWithPath:jsFilePath];
     NSString *javascriptCode1 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];
     jsFilePath = [[NSBundle mainBundle] pathForResource:@"simple2" ofType:@"js"];
     jsURL = [NSURL fileURLWithPath:jsFilePath];
     NSString *javascriptCode2 = [NSString stringWithContentsOfFile:jsFilePath encoding:NSUTF8StringEncoding error:nil];
     [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@\n%@",javascriptCode1, javascriptCode2]];
}