js拆分字符串,并给每个都添加上标签

js拆分字符串,并给每个都添加上标签

<div class=" js-m words-js2">技术产品解决方案div>
<div class=" js-m words-js">Providing first-rate technology solutions for new energy productsdiv>
<script>
  (function($){
	function injector(t, splitter, klass, after) {
		var a = t.text().split(splitter), inject = '';
		if (a.length) {
			$(a).each(function(i, item) {
				inject += '+klass+(i+1)+'">'+item+''+after;
			});	
			t.empty().append(inject);
		}
	}
	
	var methods = {
		init : function() {

			return this.each(function() {
				injector($(this), '', 'char', '');
			});

		},

		words : function() {

			return this.each(function() {
				injector($(this), ' ', 'word', ' ');
			});

		},
		
		lines : function() {

			return this.each(function() {
				var r = "eefec303079ad17405c889e092e105b0";
				// Because it's hard to split a 
tag consistently across browsers,
// (*ahem* IE *ahem*), we replaces all
instances with an md5 hash
// (of the word "split"). If you're trying to use this plugin on that // md5 hash string, it will fail because you're being ridiculous. injector($(this).children("br").replaceWith(r).end(), r, 'line', ''); }); } }; $.fn.lettering = function( method ) { // Method calling logic if ( method && methods[method] ) { return methods[ method ].apply( this, [].slice.call( arguments, 1 )); } else if ( method === 'letters' || ! method ) { return methods.init.apply( this, [].slice.call( arguments, 0 ) ); // always pass an array } $.error( 'Method ' + method + ' does not exist on jQuery.lettering' ); return this; }; })(jQuery); $(function(){ $(".words-js").lettering(); $('.words-js').each(function(){ var _this = $(this); var _colSpan=_this.find('span'); var _len=_colSpan.length; var i=0; for(i;i<_len;i++){ _colSpan.eq(i).css({ 'transition-delay': (0.01*(i+1)+1)+'s' }); } }); $(".words-js2").lettering(); $('.words-js2').each(function(){ var _this = $(this); var _colSpan=_this.find('span'); var _len=_colSpan.length; var i=0; for(i;i<_len;i++){ _colSpan.eq(i).css({ 'transition-delay': (0.02*(i+1)+1)+'s' }); } }); });
script>

你可能感兴趣的:(js效果)