jquery - 在 objective-c 中访问 WebView 元素

标签 jquery ios uiwebview

任何人都请帮助我,我怎样才能得到所有的 textFields 值(用他们的“名字”“id”) 在加载的 UIWebView 页面中。

或者请提供一个链接,该链接描述了要传入的所有 jQuery 字符串

               "stringByEvaluatingJavaScriptFromString"

方法作为参数。

最佳答案

这是您应该使用的 JavaScript 代码:

(function(){
    var i, tf, textFields = document.getElementsByTagName("input"), result = [];
    for (i = 0; tf = textFields[i]; i++) {
        if (tf.type === "text" || tf.type === "search")
            result.push({
                "type": tf.type,
                "name": tf.name,
                "id": tf.id,
                "value": tf.value
            });
    }
    return JSON.stringify(result);
})();

您可能会发现想要添加其他输入字段类型。

这是一个 Objective-C 代码示例:

- (void)getValuesButtonTapped:(id)sender {
    NSString *resultStr = [self.webView stringByEvaluatingJavaScriptFromString:
        @"(function(){"
            "var i, tf, textFields = document.getElementsByTagName(\"input\"), result = [];"
            "for (i = 0; tf = textFields[i]; i++) {"
                "if (tf.type === \"text\" || tf.type === \"search\")"
                    "result.push({\"type\": tf.type, \"name\": tf.name, \"id\": tf.id, \"value\": tf.value});"
            "}"
            "return JSON.stringify(result);"
        "})();"
    ];
    NSArray *result = [NSJSONSerialization JSONObjectWithData:[resultStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
    NSLog(@"result: %@", result);
}

enter image description here

当您点击“获取值...”按钮时,您会收到以下日志消息:

result: ({
    id = "lst-ib";
    name = q;
    type = search;
    value = "hello world";
},
    {
    id = "";
    name = "";
    type = text;
    value = "";
},
    {
    id = "";
    name = "";
    type = text;
    value = "";
})

关于jquery - 在 objective-c 中访问 WebView 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22687251/

相关文章:

ios - iOS 8 中 Xcode 无法使用所选设备运行错误

ios - 核心数据错误 : 'Unacceptable type of value for ordered to-many relationship'

ios - UIWebView复制后如何从UIPasteboard获取RTF内容?

javascript - 一个Jquery元素引用占用多少内存?

jquery - 如何在超过最大高度后为div应用滚动条

iphone - 使用 Apple 代码签名为 Cydia 构建应用程序

iphone - 如何在新 View 类中的不同线程上加载 UIWebview?在调用对象的 presentModalViewController 之前?

javascript - IOS发送暂停视频到html5

javascript - 单击按钮时如何在js中获取事件按钮

javascript - 操纵 <td >'s within different <tr>' s