gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:55:22 24 4
gpt4 key购买 nike

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 来使用字符串将 xy 坐标从 jQuery 存储到 NSString

最佳答案

根据 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

关于javascript - 如何从 jQuery 代码中获取坐标值 (x,y) 并存储在 NSString 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30926343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com