为了账号安全,请及时绑定邮箱和手机立即绑定

显示选定的单词[Javascript]

显示选定的单词[Javascript]

Qyouu 2021-05-12 16:15:44
用鼠标单击时,句子中的单词会突出显示。它在这里工作。但; 不幸的是,用按钮显示所选的单词是行不通的。JSFiddlewords = [];var sentence = $('.sentence').html().split(/\s+/),  result = [];for (var i = 0; i < sentence.length; i++) {  result[i] = '<span>' + sentence[i] + '</span>';  var a = sentence[i];}$('.sentence').html(result.join(' '));if (a.indexOf(" ") + 1) {  alert('fail: ' + a);} else {  words.push(a);}$('.sentence').on('click', 'span', function() {  $(this).addClass('highlight');});$('button').click(function() {  alert(words);});$('$selectedWords').innerHTML = words;.sentence span:hover,.highlight {  background: red;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><p class="sentence">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Similique aliquam totam odit excepturi reiciendis quam doloremque ab eius quos maxime est deleniti praesentium. Quia porro commodi blanditiis iure dicta voluptatibus.</p>Selected words:<p id="selectedWords"></p><button>Show in alert</button>
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信