加入收藏 | 设为首页 | 会员中心 | 我要投稿 西安站长网 (https://www.029zz.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 移动 > 正文

推荐10 个短小却超实用的 JavaScript 代码段

发布时间:2019-07-03 14:53:58 所属栏目:移动 来源:大灰狼的小绵羊哥哥
导读:副标题#e# JavaScript正变得越来越流行,它已经成为前端开发的第一选择,并且利用基于JavaScript语言的NodeJS,我们也可以开发出高性能的后端服务,甚至我还看到在硬件编程领域也出现了JavaScript的身影。JavaScript正在逐渐进化为一门全能的开发语言。 但

你同样会需要取消高亮的函数:

  1. function unhighlight(text, tag) { 
  2. // Default tag if no tag is provided 
  3. tag = tag || 'span'; 
  4. var re = new RegExp('(<'+ tag +'.+?>|)', 'g'); 
  5. return text.replace(re, ''); 

使用方法:

  1. $('p').html( highlight( 
  2. $('p').html(), // the text 
  3. ['foo', 'bar', 'baz', 'hello world'], // list of words or phrases to highlight 
  4. 'strong' // custom tag 
  5. )); 

4. 文字动效

有时你会希望给你的一段文字增加动效,让其中的每个字都动起来。你可以使用下面这段jQuery插件代码来达到这个效果。当然你需要结合一个CSS3 transition样式来达到更好的效果。

  1. $.fn.animateText = function(delay, klass) {  
  2. var text = this.text();  
  3. var letters = text.split('');  
  4. return this.each(function(){  
  5. var $this = $(this);  
  6. $this.html(text.replace(/./g, '$&'));  
  7. $this.find('span.letter').each(function(i, el){  
  8. setTimeout(function(){ $(el).addClass(klass); }, delay * i);  
  9. });  
  10. });  
  11. }; 

使用方法:

  1. $('p').animateText(15, 'foo'); 

5. 逐个隐藏元素

(编辑:西安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读