如何打破html中的长词?

问题描述:

我在javascript中动态创建列表。我想打破点点(.....)的长字。例如,如果单词是“软件生命周期开发”,我想将它改为“软件生命周期....”。如何打破html中的长词?

我用“WORD-BREAK:BREAK-ALL; white-space:normal;” 我目前的输出是:

软件生命周期 开发。

谁能告诉,如何解决这个问题?提前致谢。

var parent = document.getElementById('searchlistview'); 
var listItem = document.createElement('li'); 
listItem.setAttribute('id', 'listitem_' + listId); 
listItem.setAttribute('data-icon', 'false'); 
listItem.innerHTML = "<img src=" + imagesrc + " class='ui-li-icon ui-li-has-icon'></img> 
<a target='_black' data-role='button' href='#' id=" + listId + " name= " + url + " data-theme ='c' rel='external' 
data-inline='true' style='margin-left:0em; WORD-BREAK:BREAK-ALL;white-space:normal; ' >" + searchName + "<p style='margin-top:1px;margin-left:1px;'> 
File size: " + fileSize + "</p></a>"; 
parent.appendChild(listItem); 
+0

您是否尝试过psuedo算法:if length-of-string> n string Kris 2011-12-26 11:46:15

+0

@Krish:我从来没有听说过伪码算法。你能用例子多说一点吗? – selladurai 2011-12-26 11:54:28

+0

[如果内容太宽,将省略号(...)插入到HTML标记中的可能的重复](http://*.com/questions/536814/insert-ellipsis-into-html-tag-if-content-too-wide ) – outis 2011-12-26 12:03:16

找到更多的细节你可以试试这个CSS

text-overflow:ellipsis; 

这将使...当文本溢出。看到这个page供您参考。

+0

不适用于所有浏览器。 – DanMan 2011-12-26 13:26:17

尝试:

function wbr(str, num) { 
    return str.replace(RegExp("(\\w{" + num + "})(\\w)", "g"), function(all,text,char){ 
    return text + "<wbr>" + char; 
    }); 
} 

来源:http://ejohn.org/blog/injecting-word-breaks-with-javascript/

+1

请确认你的来源 – 2011-12-26 11:48:31

style="text-overflow: ellipsis;" 

将解决这个问题。

+0

是那个html? CSS? – Kris 2011-12-26 11:57:12

+0

@Kris它的CSS。 – 2011-12-26 12:02:33