我正在构建一个JS脚本,在某个点上,它可以在给定页面上允许用户单击任何单词并将该单词存储在变量中.
我有一个非常难看的解决方案,并使用jQuery进行类解析:我首先解析整个html,拆分每个空间的所有内容" ",并重新附加包含在a中的所有内容<span class="word">word</span>,然后我添加一个带有jQ的事件来检测点击次数这样一个类,并使用$(this).innerHTML我得到点击的单词.
这在很多方面都是缓慢而丑陋的,我希望有人知道另一种方法来实现这一目标.
PS:我可能会考虑将其作为浏览器扩展程序运行,所以如果单纯的JS听起来不可能,并且如果您知道允许的浏览器API,请随时提及它!
一个可能的owrkaround将是让用户突出显示单词而不是单击它,但我真的希望能够通过只需单击即可实现相同的功能!
我有一个HTML文档,其中包含大约30,000个单词.
我希望能够在用户点击任何单词时执行某些操作.为了简单/概念,我只想alert()说这个词.
例如,在段落中,如果我点击"有"它应该运行alert("have").
我正在使用jQuery.
在HTML文档中,如何通过单击获得单词?
例如,如果我有这个:
<div id = "word">
hello world
</div>
Run Code Online (Sandbox Code Playgroud)
我希望能够点击worddiv中的任何单词并在没有选择的情况下获取它.这可能是使用JQuery吗?
我有:简单的html文本块:
<p>
The future of manned space exploration and development of space depends critically on the
creation of a dramatically more proficient propulsion architecture for in-space transportation.
A very persuasive reason for investigating the applicability of nuclear power in rockets is the
vast energy density gain of nuclear fuel when compared to chemical combustion energy...
</p>
Run Code Online (Sandbox Code Playgroud)
我想:当用户点击它时,将单词换成span.
即用户点击了载人字,而不是我应该得到的
<p>
The future of <span class="touched">manned</span> space exploration and development of space depends critically on the
creation …Run Code Online (Sandbox Code Playgroud)