跨浏览器的word breaker

//aka makeDesignerHappy(dEl);
function breakWord(dEl){


if(!dEl || dEl.nodeType !== 1){

return false;

} else if(dEl.currentStyle && typeof dEl.currentStyle.wordBreak === 'string'){

//Lazy Function Definition Pattern, Peter's Blog
//From http://peter.michaux.ca/article/3556

breakWord = function(dEl){
//For Internet Explorer
dEl.runtimeStyle.wordBreak = 'break-all';
return true;
}

return breakWord(dEl);

}else if(document.createTreeWalker){

//Faster Trim in Javascript, Flagrant Badassery
//http://blog.stevenlevithan.com/archives/faster-trim-javascript

var trim = function (str) {
str = str.replace(/^ss*/, '');
var ws = /s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}

//Lazy Function Definition Pattern, Peter's Blog
//From http://peter.michaux.ca/article/3556

breakWord = function(dEl){

//For Opera, Safari, and Firefox
var dWalker = document.createTreeWalker(dEl, NodeFilter.SHOW_TEXT, null, false);
var node,s,c = String.fromCharCode('8203');
while (dWalker.nextNode())
{
node = dWalker.currentNode;
//we need to trim String otherwise Firefox will display
//incorect text-indent with space characters
s = trim( node.nodeValue ) .split('').join(c);
node.nodeValue = s;
}
return true;
}

return breakWord(dEl);


}else{
return false;
}
}


转自: http://www.hedgerwow.com/360/dhtml/css-word-break.html
This article is posted by on , link is .

Leave a reply