JavaScript 中的位置
你好,我的代码有问题.. 为什么它不起作用..? 我的代码有问题吗?
function selectWord() {
var select = window.getSelection();
if (select.getBoundingClientRect) {
var rect = select.getBoundingClientRect ();
x = rect.left;
y = rect.top;
w = rect.right - rect.left;
h = rect.bottom - rect.top;
alert (" Left: " + x + "\n Top: " + y + "\n Width: " + w + "\n Height: " + h);
}
else {
alert ("Your browser does not support!");
}
}
谢谢
hello i have a problem with my code..
why it doesn't work..??
is there fault with my code?
function selectWord() {
var select = window.getSelection();
if (select.getBoundingClientRect) {
var rect = select.getBoundingClientRect ();
x = rect.left;
y = rect.top;
w = rect.right - rect.left;
h = rect.bottom - rect.top;
alert (" Left: " + x + "\n Top: " + y + "\n Width: " + w + "\n Height: " + h);
}
else {
alert ("Your browser does not support!");
}
}
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是您在不支持它的浏览器中使用它。
IE 不支持 getSelection,Fx 3.7 应该是第一个支持 getBoundingClientRect
Firefox 的 getBoundingClientRect 问题
My guess is you are using this in a browser that does not support it.
IE does not support getSelection and Fx 3.7 should be the first one to support the getBoundingClientRect
getBoundingClientRect problem with Firefox
getBoundingClientRect 是一个 DOM Node 方法,getSelection 方法的结果不是 DOM Node。
可能有一种方法可以使用返回的 Selection 的anchorNode、anchorOffset、focusNode 和 focusOffset 属性。
如果您使用 firefox + firebug,您可以执行 console.log(select) 并检查您有权访问的属性。
getBoundingClientRect is a DOM Node method, the result of the getSelection method is not a DOM Node.
There might be a way using the anchorNode, anchorOffset, focusNode and focusOffset properties of the Selection that is returned.
If you use firefox + firebug, you can do console.log(select) and inspect the properties that you have access to.