How to get position of an element in UIWebView?

后端 未结 3 451
情书的邮戳
情书的邮戳 2021-02-01 23:50

I have UIWebView loaded with html in my iPad Program. By using -webkit-column-width, I divided the html with several columns.

padding: 0px
height: 1024px
-webkit         


        
相关标签:
3条回答
  • 2021-02-02 00:05

    Here is the swift 4 version of @JohanKools answer https://stackoverflow.com/a/8307454

    func positionOfElement(withId elementID: String?) -> CGRect 
    {
        let js = "function f(){ var r = document.getElementById('%@').getBoundingClientRect(); return '{{'+r.left+','+r.top+'},{'+r.width+','+r.height+'}}'; } f();"
        let result = webView?.stringByEvaluatingJavaScript(from: String(format: js, elementID))
        let rect: CGRect = CGRectFromString(result!)
        return rect
    }
    
    0 讨论(0)
  • 2021-02-02 00:09

    Because having to include jQuery just for this sucks:

    - (CGRect)positionOfElementWithId:(NSString *)elementID {
        NSString *js = @"function f(){ var r = document.getElementById('%@').getBoundingClientRect(); return '{{'+r.left+','+r.top+'},{'+r.width+','+r.height+'}}'; } f();";
        NSString *result = [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:js, elementID]];
        CGRect rect = CGRectFromString(result);
        return rect;
    }
    
    0 讨论(0)
  • 2021-02-02 00:19

    Ok, i solved using jquery function

    var p  = $("tag");
    var position = p.position();
    

    I gets real x, y values. Thanks.

    0 讨论(0)
提交回复
热议问题