【问题标题】:How to Fetch Coordinate Value (x,y) from jQuery Code and Store in NSString?如何从 jQuery 代码中获取坐标值(x,y)并存储在 NSString 中?
【发布时间】:2015-06-18 21:45:50
【问题描述】:

div标签用于在UIWebview中进行bolditalic等操作。当用户通过 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 来使用字符串将 xy 坐标从 jQuery 存储到 NSString

【问题讨论】:

  • 改进了格式,修正了拼写错误以提高可读性。

标签: javascript jquery ios objective-c uiwebview


【解决方案1】:

根据UIWebView Class Reference,您需要使用stringByEvaluatingJavaScriptFromString 并从您的Javascript 函数中返回一个值。

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

根据评论更新:

jQuery 只是一个 Javascript 库。基本原理仍然适用。要在 jQuery 中返回一个值(在这种情况下,更改您提供的函数),您只需要......好吧...... 返回该值。

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

【讨论】:

  • 我想从 Jquery 返回值。怎么做。我已经知道 Javascript 和一些函数只能在 jquery 上运行良好。
  • 查看我的更新答案。返回一个值是非常标准的东西。
猜你喜欢
  • 2022-01-19
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多