Javascript:在选择时切换文本
Javascript: Switch text on select
我有:简单的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>
问题:如何在JavaScript的帮助下在下面(在现有文本行之间)插入一个概念?
即我想用几句话解释上面文本nuclear power
的内容,所以我想在该短语下方插入几个单词(例如鼠标悬停)。
这将做到这一点。如果您需要更多详细信息,请告诉我。
<!DOCTYPE html>
<html>
<head></head>
<body>
<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>
<script>
var p = document.getElementsByTagName('p');
p = p[0];
var tmp = '';
p.addEventListener('mouseover', function(){
tmp = p.innerHTML;
p.innerHTML = p.innerHTML + ' ...about nuclear...';
}, false);
p.addEventListener('mouseout', function(){
p.innerHTML = tmp;
}, false);
</script>
</body>
</html>
这是一个带有可用于任何元素的通用函数的版本。
<!DOCTYPE html>
<html>
<head>
<script>
var switchText = function(element, text){
var tmp = '';
element.addEventListener('mouseover', function(){
tmp = element.innerHTML;
element.innerHTML = element.innerHTML + text;
}, false);
element.addEventListener('mouseout', function(){
element.innerHTML = tmp;
}, false);
};
</script>
</head>
<body>
<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>
<script>
var p = document.getElementsByTagName('p');
switchText(p[0], '...about nuclear...');
</script>
</body>
</html>
将其插入单词旁边。我使用了带有红色的 span 标签来强调。您当然可以添加您喜欢的任何内容。
<!DOCTYPE html>
<html>
<head>
<script>
var switchText = function(element, words, text){
var tmp = '';
element.addEventListener('mouseover', function(){
tmp = element.innerHTML;
element.innerHTML = element.innerHTML.replace(words, function(m){
return m+'<span class="nuc">'+text+'</span>';
});
}, false);
element.addEventListener('mouseout', function(){
element.innerHTML = tmp;
}, false);
};
</script>
<style>
.nuc{ color: red; }
</style>
</head>
<body>
<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>
<script>
var p = document.getElementsByTagName('p');
switchText(p[0], /nuclear/g, '...about nuclear...');
</script>
</body>
</html>
我还在 switchText
函数中添加了 words 参数,使其也更加通用。
第四个,也是最后一个条件已经解决。以下内容将允许在特定悬停的单词或短语上显示文本。
<!DOCTYPE html>
<html>
<head>
<script>
var switchText = function(element, words, text){
element.innerHTML = element.innerHTML.replace(words, function(m){
return '<p class="nuc">'+m+'</p>';
});
var nuc = element.getElementsByClassName('nuc');
for(var i=0; i<nuc.length; i++){
(function(nuc){
var tmp = nuc.innerHTML,
doing = false;
nuc.addEventListener('mouseover', function(){
if(!doing)
this.innerHTML = this.innerHTML + text;
doing = true;
});
nuc.addEventListener('mouseout', function(){
this.innerHTML = tmp;
doing = false;
});
})(nuc[i]);
}
};
</script>
<style>
.nuc{ color: red; display: inline;}
</style>
</head>
<body>
<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>
<script>
var p = document.getElementsByTagName('p');
switchText(p[0], /nuclear/g, '...about nuclear...');
</script>
</body>
</html>
提醒一下,将这种方法用于某些类型的CSS positioning
可以制作styled popups
、inline navigation
,以及可能具有奇怪行为的其他盒子。
相关文章:
- 或者在表单上选择默认选项文本(选择1)
- Javascript:包括特殊字符的文本选择
- 大写--文本区域中的文本选择
- 点击
- 项目时的文本选择
- 通过 javascript 在每次点击时动态禁用文本选择
- 文本框的 CSS 文本选择覆盖
- 单击“img”源时,文本选择未清除
- 链接内的内容可编辑块,如何防止重定向,但让文本选择插入符号位置更改
- 当我单击画布并拖动鼠标时,光标将变为文本选择光标.我该如何防止这种情况发生
- 重新启用文本选择(Opera、webkit等)
- 绝对元素后面的元素上的文本选择
- 禁用JavaScript中的文本选择器
- 从网站中删除文本选择限制的脚本
- Javascript/jQuery Load 函数,文本选择
- HTML 浏览器的“文本”选择:选择整个单词(如在 iBooks 中)
- 锁定页面文本选择
- 如何删除文本选择中的子元素
- 如何将选定的“文本”选择框发送到按钮上的函数,以便使用角度进行 ng 单击
- 锤击.JS桌面文本选择和滑动冲突
- j查询启用/禁用文本选择错误