2011-09-13 22 views
14

我在我的iPad程序中加載了HTML的UIWebView。通過使用-webkit-column-width,我將html分成了幾列。如何獲取UIWebView中元素的位置?

padding: 0px 
height: 1024px 
-webkit-column-gap: 0px 
-webkit-column-width: 768px 

我怎樣才能在webview中獲取元素位置(x,y)?位置在屏幕上,而不是在html中。所以y的範圍從0到webview的高度。

在此先感謝。

+0

這是不一樣的你在http://stackoverflow.com/questions/7378102/using-css3-columns-how-can問題-get-元件位置/ 7429994? – chris5marsh

回答

2

好吧,我解決了使用jquery功能

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

我得到真正的x,y值。謝謝。

+0

我如何從位置var獲取x,y? – harshitgupta

33

因爲不必包括jQuery的只是這個吮吸:

- (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

我如何使用document.getElementByTagName執行相同操作? – nidIOS

+0

這太棒了!有沒有一個函數來設置boundingClientRect? – tbag