简体   繁体   English

检测在链接内点击了哪个词

[英]detect which word has been clicked inside link

I'm working on a chrome extension that extracts every word clicked.我正在开发一个 chrome 扩展程序,它可以提取点击的每个单词。 This code works great for any word outside of a link:此代码适用于链接之外的任何单词:

 $(document).click(function(e) { var t = ''; var s = window.getSelection(); if (s.isCollapsed) { s.modify('move', 'forward', 'character'); s.modify('move', 'backward', 'word'); s.modify('extend', 'forward', 'word'); t = s.toString(); s.modify('move', 'forward', 'character'); } else { t = s.toString(); } console.log(t); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#">foo bar</a><br> words not in link

However I also need to get the words clicked inside a link.但是,我还需要在链接内单击单词。 so for example with this html:因此,例如使用此 html:

<a href="#">foo bar</a>

I need to get "foo" when the word foo is clicked.单击 foo 这个词时,我需要得到“foo”。 Is there a way do to that with jQuery?有没有办法用 jQuery 做到这一点?

I found a way to see which character is clicked within a link.我找到了一种方法来查看在链接中单击了哪个字符。 This also takes you to the webpage you have set for it to go to这也会将您带到您为其设置的网页

var link = prompt('what would you like as a link');
var href = prompt("where would you like the link to go")
var splitLink = link.split('');
for(var i = 0;i<splitLink.length;i++){
  $('body').append('<a target="_blank">'+splitLink[i]+"</a>");
}
$('a').click(function(){
  alert('you clicked the '+$(this).html());
  window.open(href)
})

and if you wish you can also do this for multiple words if you wish to see which word is clicked如果您希望查看单击了哪个单词,也可以对多个单词执行此操作

var link = prompt('what would you like as a link');
var href = prompt("where would you like the link to go")
var splitLink = link.split(' ');
for(var i = 0;i<splitLink.length;i++){
  $('body').append('<a target="_blank">'+splitLink[i]+" </a>");
}
$('a').click(function(){
  alert('you clicked the '+$(this).html());
  window.open(href)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM