菜鸟教程小白 发表于 2022-12-12 13:02:05

c# - 在 UIWebView 中调用 javascript 命令


                                            <p><p>我想在<code>UIWebView</code>页面中调用一个<code>javascript</code>命令如:</p>

<pre><code>javascript:document.getElementById(\&#34;MainContent_btnApprove\&#34;).click();
</code></pre>

<p>但是我似乎无法让它工作。这是我目前所拥有的:</p>

<pre><code>string url = &#34;javascript:document.getElementById(\&#34;MainContent_btnApprove\&#34;).click();&#34;;
WebView.ScalesPageToFit = true;
WebView.LoadRequest(new NSUrlRequest(new NSUrl(url)));
WebView.ScalesPageToFit = true;
</code></pre>

<p>但随后我收到一条消息:</p>

<blockquote>
<p>System.Exception: Could not initialize an instance of the web &#39;Foundation.NSUrl&#39;: the native &#39;initWithString:&#39; method returned nil.</p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>ObjC <code>NSURL</code> 的选择器 <code>initWithString:</code> 如果值不是正确/有效的 URL(并且会有所不同),则返回 <code>nil</code> iOS 版本之间)。您得到的异常(exception)是 Xamarin.iOS 告诉您 iOS 本身不喜欢您提供的 <code>url</code> 值。</p>

<p>根据您的描述,我认为您正在寻找类似的东西:</p>

<pre><code>WebView.EvaluateJavascript (&#34;document.getElementById(\&#34;MainContent_btnApprove\&#34;).click();&#34;);
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于c# - 在 UIWebView 中调用 javascript 命令,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27158830/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27158830/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - 在 UIWebView 中调用 javascript 命令