js实现单击英文文本取词
源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>单击英文文本取词</title> </head> <body> <div>If you visit the Great Smoky Mountains National Park, chances are you will remember the roadside or campground bears above all else. Bears are the most popular animals in a number of our national parks. In these mountains where the population of bears runs into the hundreds, opportunities to observe these large wild animals are plentiful during the summer. Since national parks are wildlife sanctuaries where no disturbance of the native animals is allowed, years of protection have served to break down the wild bears' fear of humans. Now, instead of depending on their own resources for a living, many bears patrol park roads and campgrounds.</div> <script> var obj = document.querySelector('div'); var s1 = obj.innerHTML; var s2 = s1.replace(/\b(\w+?)\b/g, '<span class="word">$1</span>'); obj.innerHTML = s2; var sps = document.querySelectorAll('.word'); sps.forEach((item, index) => { item.onclick = function(event) {alert(event.target.innerHTML)}; }) </script> </body> </html> |