如何附加“选择” 页面上每个文本节点的事件? 有没有更好的办法?
注意:似乎解决了我自己的问题。 查看编辑内容。
我正在尝试编写一个greasemonkey 脚本,当用户选择文本时,它将突出显示页面上的所有匹配文本,然后在取消选择时删除突出显示。 我正在寻找一种 jQuery 方法来做到这一点。 似乎 jQuery 核心中有一个 select 事件,但我无法让它按照我想要的方式工作。 例如,尝试在此页面上运行以下命令:
$(document).select(function () {
console.log("hey");
});
没有响应。 所以看来我需要特别将其附加到文本节点。 但这对我来说似乎不切实际。 那就太慢了。
有什么建议、想法吗? 谢谢。
编辑#1:Ken Browning 指出 jQuery 中的 select 事件仅在文本区域和输入字段中触发。 所以我想我必须放弃它。 也许我可以稍微改变我的问题,并询问是否有人可以告诉我将选择事件绑定到所有文本节点的javascript方法。
编辑#2:我想我找到了部分答案。 这将返回页面上选定的文本,但您仍然需要单击按钮才能显示它。 没有选择事件绑定。 嗯。
<script language=javascript>
function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{//javascript select event
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
document.aform.selectedtext.value = txt;
}
</script>
<input type="button" value="Get selection" onmousedown="getSelText()">
http://www.codetoad.com/javascript_get_selected_text.asp
编辑#3:所以我绑定到 mouseup 事件而不是选择。 然后,我检查 getSelText() 的值,以便在该点之后进一步继续。 耶!
编辑#4:如果有人好奇,我将完成的脚本发布在用户脚本上。 链接在这里。 享受!
NOTE: Solved my own problem, it seems. See the edits.
I am trying to write a greasemonkey script that will highlight all the matching texts on the page when the user selects a text, and then remove the highlighting when the selection is canceled. I am looking for a jQuery way to do this. It seems like there is a select event in the jQuery core, but I couldn't get it to work they way I wanted it. Try running the following on this page for instance:
$(document).select(function () {
console.log("hey");
});
There is no response. So it seems that I need to attach this to text nodes in particular. But that doesn't seem practical to me. It would be way too slow.
Any suggestions, ideas? Thanks.
EDIT # 1:Ken Browning pointed out that the select event in jQuery only fires in textarea and input fields. So I guess I have to abandon that. Maybe I can change my question slightly, and ask if anyone can tell me the javascript way to bind selection events to all the text nodes.
EDIT # 2: I think I found part of my answer. This returns the selected text on the page, but you still have to click a button to show it. There is no select event binding. Hm.
<script language=javascript>
function getSelText()
{
var txt = '';
if (window.getSelection)
{
txt = window.getSelection();
}
else if (document.getSelection)
{//javascript select event
txt = document.getSelection();
}
else if (document.selection)
{
txt = document.selection.createRange().text;
}
else return;
document.aform.selectedtext.value = txt;
}
</script>
<input type="button" value="Get selection" onmousedown="getSelText()">
http://www.codetoad.com/javascript_get_selected_text.asp
EDIT # 3: SO I bound to the mouseup event rather than to selection. I am then checking the value of the getSelText() to proceed further after that point. Yay!
EDIT # 4: In case anyone is curious, I posted my finished script on Userscripts. Here is the link. Enjoy!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Firefox 中,
document.getSelection()
将返回选定的文本。您可以将
onmouseup
处理程序附加到document
,并在每次收到mouseup
事件时检查选择情况。注意:在我写完这篇文章后,您发布了一些源代码来进行选择。 虽然该代码很好(并且跨浏览器),但如果您使用 Greasemonkey,则必须使用 Firefox,因此您需要的只是
document.getSelection()
。In Firefox,
document.getSelection()
will return the selected text.You could attach an
onmouseup
handler to thedocument
, and check for the selection each time you receive amouseup
event.Note: After I wrote this, you posted some source code to get the selection. While that code is fine (and cross-browser), if you’re using Greasemonkey you must be on Firefox, so all you need is
document.getSelection()
.我读到这意味着您遇到了错误的事件,除非您的页面内容位于
或
I read that to mean you've got the wrong event unless your page contents are in an
<input type="text">
or a<textarea>