[英]Change html text color without reloading the webview
I'm wondering how can I change the html text color without reloading the webview because with the code I'm using currently, when I scroll down the webview that's holding the html file then click the button that I set to change the html text color, the page jumps again to the beginning of the html file. 我想知道如何在不重新加载webview的情况下更改html文本颜色,因为使用当前使用的代码,当我向下滚动保存html文件的webview时,单击我设置为更改html文本颜色的按钮,页面再次跳到html文件的开头。 I think that happens because the webview is being reloaded every time I click the button.
我认为是因为每次单击按钮都会重新加载Web视图。 What do you thinks guys?
你们怎么看?
The code I'm using 我正在使用的代码
NSString *myHtmlFile = [[NSBundle mainBundle] pathForResource:htmlName ofType:@"htm"];
NSString* text = [NSString stringWithContentsOfFile:myHtmlFile encoding:NSUTF8StringEncoding error:nil];
[self.myWebView loadHTMLString:[NSString stringWithFormat:@"<html><body text=\"#000000\" face=\"Bookman Old Style, Book Antiqua, Garamond\" size=\"5\">%@</body></html>", text] baseURL: nil];
I don't have any experience in javascript so if you can simplify your answer, it would be very helpful. 我没有使用javascript的经验,因此,如果您可以简化答案,那将非常有帮助。
What I have noticed is that for changing the text color of an already loaded html file, using the following code is better as it doesn't cause the webview to reload. 我注意到的是,对于更改已经加载的html文件的文本颜色,使用以下代码会更好,因为它不会导致重新加载Webview。 But what worth noting is that this code only worked for me when changing an already loaded html file.
但值得注意的是,此代码仅在更改已加载的html文件时才对我有用。
[_myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.color =\"Black\""];
But when I wanted to change the text color of a new html file (being loaded), the following code worked better for me. 但是,当我想更改新的html文件的文本颜色(正在加载)时,以下代码对我来说效果更好。
NSString * myHtmlFile = [[NSBundle mainBundle] pathForResource:htmlName ofType:@"htm"];
NSString* text = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",htmlFile);
[self.myWebView loadHTMLString:[NSString stringWithFormat:@"<html><body text=\"#000000\" face=\"Bookman Old Style, Book Antiqua, Garamond\" size=\"5\">%@</body></html>", text] baseURL: nil];
当您添加标签时,这意味着webview会将其视为新页面并重新加载,您可以使用javascript或jQuery实现您的任务:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.