如何从 jQuery 代码中获取坐标值 (x,y) 并存储在 NSString 中?

How to Fetch Coordinate Value (x,y) from jQuery Code and Store in NSString?

div标签用于在UIWebview中执行粗体斜体等操作。当用户通过 JavaScript/jQuery.

触摸 div 标签时,我想获取位置坐标

我在 JSFiddle 中找到了代码。

$(document).ready(function(e) {
    $('#C').click(function(e) {
        var posX = $(this).position().left, posY = $(this).position().top;
        var x =  e.pageX - posX;
        var y =  e.pageY - posY;
        alert( (x) + ' , ' + (y));
    });
});
#C { 
    width: 100px;
    height: 100px;
    cursor: pointer;
    background: #2f2f2f;
    position: absolute;
    top: 50px;
    color: #fff;
    font: bold 15px Arial; 
}
<div id="C" style="left:500px;">
    position() <br /> 
    mouse <br/> 
    position 
</div>

如何通过从字符串计算 JavaScript 来使用字符串存储从 jQuery 到 NSStringxy 坐标?

根据 UIWebView Class Reference,您需要使用 stringByEvaluatingJavaScriptFromString 和 return 来自 Javascript 函数的值。

This question on Stack Overflow 有一些答案非常详细。我建议从那里开始。

更新回复评论:

jQuery 只是一个 Javascript 库。基本原理仍然适用。要 return jQuery 中的值(在本例中,更改您提供的函数),您只需要...好吧... return 值。

var getCoord = function() {
    var posX = $(this).position().left, posY = $(this).position().top;
    var x =  e.pageX - posX;
    var y =  e.pageY - posY;
    return (x) + ' , ' + (y); // this is the line that returns your coordinates
};

getCoord(); // to call your function