Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios使用javascript使用objective c中的值填充webview_Javascript_Iphone_Ios - Fatal编程技术网

ios使用javascript使用objective c中的值填充webview

ios使用javascript使用objective c中的值填充webview,javascript,iphone,ios,Javascript,Iphone,Ios,我需要用存储在objective C中的变量填充html Web视图 我相信我需要使用: [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"John", firstName]; 但我不确定该放什么,我的javascript需要什么来实现传递的变量 以下是我的一点html,有助于了解: <ul><li><span>First Name:</s

我需要用存储在objective C中的变量填充html Web视图

我相信我需要使用:

[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"John", firstName];
但我不确定该放什么,我的javascript需要什么来实现传递的变量

以下是我的一点html,有助于了解:

<ul><li><span>First Name:</span> first name needs to go here</li>
  <li><span>Last Name:</span> last name needs to go here</li>
更新2: javascript/html:

<ul><li><span>Policy Number:</span>        
<script> function myFunc(str)
        {
            document.write(str)  }
        </script>
  • 保单编号: 函数myFunc(str) { document.write(str)}
基本上,您需要一个javascript方法将数据添加到HTML字符串中。一旦有了这个函数,向它传递数据就相当简单了

NSString *script = [NSString stringWithFormat:@"methodName([%@])", data]
[self.webView stringByEvaluatingJavaScriptFromString:script];

编写Javascript以将数据放入HTML字符串是非常困难的

您有权访问代码中的原始HTML字符串吗?如果是这样的话,你能做类似的事情吗

UIWebView *webview = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webview];
NSString *htmlString = [NSString stringWithFormat:@"<html>\
<ul><li><span>First Name:</span> %@</li>\
<li><span>Last Name:</span> %@</li>\
</html>", @"John", @"Smith"];
[webview loadHTMLString:htmlString baseURL:nil];
UIWebView*webview=[[UIWebView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:webview];
NSString*htmlString=[NSString stringWithFormat:@]\
  • 名字:%@
  • \
  • 姓氏:%@
  • \ “,”约翰“,”史密斯“; [webview加载htmlString:htmlString基URL:nil];
如果您想使用JavaScript,下面是一个示例:

我最终使用了不同的js方法,希望这对其他人有所帮助

obj c:

NSString *str  = @"John";
NSString *script = [NSString stringWithFormat:@"document.getElementById('name').innerHTML = '%@';", str];
[self.webView stringByEvaluatingJavaScriptFromString:script];
js:

  • 名字:名字在这里

  • methodName是javascript函数名吗?我的html中有一个空白输出。你能看看我更新的代码,看看哪里出了问题吗?很抱歉,你还没有写一个方法。看看这个(使用JQuery,不过它应该会给你一个想法。我再次更新了我的javascript,现在当视图出现时,html消失了,我留下了一个空白屏幕,只有我传递的变量名。(在本例中为“John”)还将methodName([%@])更改为('%@)不确定这是否会影响任何事情尽管这会而且确实有效,但添加所有适当的引号等以获得正确的格式将是非常耗时的。
    NSString *str  = @"John";
    NSString *script = [NSString stringWithFormat:@"document.getElementById('name').innerHTML = '%@';", str];
    [self.webView stringByEvaluatingJavaScriptFromString:script];
    
    <li><span>Name:</span>   <span id = 'name'>name goes here</span>
          </li>