Word Breaker - JQueryで連続した半角英数字を任意の場所で改行する

IE系には、独自CSS word-break: break-all を適用し、それ以外のブラウザにはテキストの1文字ごとに任意改行文字(8203)をはさむプラグイン



Word Breaker jquery.wb.js - 0.2
2009/12/10 version 0.2
グローバル変数を使わないように変更
テキスト要素の無いタグは再帰しないように変更
FireFox2 の場合は <wbr /> を挿入するように変更
$.trim で無駄な余白を削除するよう変更
$.browser 非推奨関数を使うのをやめた

  jQuery(function($){
 
    $.wb = {
      
      version: 0.2
         
     ,sep: String.fromCharCode(8203)
      
     ,remake: function(elm){
       var h = elm.contents();
               elm.html('');
       $(h).each(function(){
         if(this.nodeType == 3){
           elm.append($.trim(this.textContent.split('').join($.wb.sep)));
         }else if(this.textContent == ''){
           elm.append(this);
         }else{
           elm.append($.wb.remake($(this)));
         }
       });
       return elm;
     }
    }
    
    $.fn.wb = function(){
      if(/msie/i.test(navigator.userAgent)){
        $(this).css('word-break', 'break-all');
      }else{
        $.wb.sep =  (/firefox\/2/i.test(navigator.userAgent)) ? '<wbr />' : String.fromCharCode(8203);
        $(this).each(function(){ $.wb.remake($(this));});
      }

      return $(this);
    }
  });



任意の要素を指定して実行してください
  jQuery(function($){
    $('.word-break').wb();      
  });


■レイアウトが崩れる例 table{width:500px} td{width:50%}
ddddddddddyyyyyyyyaaaaaaaaaaabbbbbbeeeeebbbbbbbbbbbbbbbbbcccccccccccccccccchhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh あああああああああああああああああああああああああああああああああああああ


■Word Breker 適用 table{width:500px} td{width:50%}
ddddddddddyyyyyyyyaaaaaaaaaaabbbbbbeeeeebbbbbbbbbbbbbbbbbcccccccccccccccccchhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh あああああああああああああああああああああああああああああああああああああ



関連プログラム・質問や感想はこちら
JQueryで連続した半角英数字を任意の場所で改行するプラグイン【bushimichi 日記】
jQueryでSelectの表示を変えるプラグイン【bushimichi 日記】

copyright © 2008 bushimichi