要调用哪个事件处理程序来选择Google字典Chrome扩展等元素? [英] Which event handler to call to select element like Google dictionary Chrome extension?

查看:214
本文介绍了要调用哪个事件处理程序来选择Google字典Chrome扩展等元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Chrome浏览器扩展程序,当用户选择一段文本时,该扩展程序必须显示弹出式窗口,就像Google Chrome字典插件一样。我一直在寻找很多,但找不到任何方法。我想这样做一个弹出窗口,选中的文本也应该出现在弹出窗口中,我已经在JS中尝试了一个onselect(),但是无法获得所需的内容。





任何人都可以在伪代码或算法的形式?确切的代码是不需要的;例如,欢迎。

我的代码是错误的,完全基于点击事件监听器是行不通的,我需要一个更好的事件处理程序,只有当文本

  document.body.addEventListener(click,function(){
var selObj = window。 getSelection();
var selectedText = selObj.toString();
alert(selectedText);

,false);


解决方案

select事件仅适用于输入元素和textarea元素。
所以你可以在你的html中使用其中的一个,并使用一些CSS来使它看起来像一个普通的元素。



或者你可以创建一些函数触发mousedown和mouseup,然后查看Selection API以检查用户是否选择了一些文本。
https://developer.mozilla.org/nl/docs/Web / API /选择


I'm making a Chrome browser extension that has to display popup when a user selects a piece of text just like this Google Chrome dictionary plugin.I've been searching a lot but couldn't find any method. I want to make a popup like this and the text selected should also appear in the popup , I've tried a onselect() in JS but couldn't get what is needed.

Can anyone explain in the form of a pseudo code or algorithm? Exact code isn't required; examples are welcome.

My code which is faulty and is based purely on "click" event listener is not working , I need a better event handler which only triggers when the text is selected.

  document.body.addEventListener("click", function(){
  var selObj = window.getSelection();
  var selectedText = selObj.toString();
  alert(selectedText);

}, false);

解决方案

The select event is only available for input elements and textarea elements. So you could either use one of those in your html and use some css to make it look like a normal element.

Or you could create some function that triggers on mousedown and mouseup and then looks at the Selection API to check if your user selected some text. https://developer.mozilla.org/nl/docs/Web/API/Selection

这篇关于要调用哪个事件处理程序来选择Google字典Chrome扩展等元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆