Javascript 如何强制断开不易断开的绳子?

Javascript 如何强制断开不易断开的绳子?,javascript,html,css,Javascript,Html,Css,我有一个HTML页面,我从数据库中包含的数据生成。数据库有时包含浏览器无法中断的长字符串,因为这些字符串不包含可中断字符(空格、点、逗号等) 有没有办法用html、css甚至javascript来解决这个问题 查看此问题的示例。正如已经多次指出的,不,如果不在显示字符串之前以编程方式预处理字符串,您将无能为力 我知道有一种策略可以在需要的地方插入软连字符(­;),但这似乎不是一种流行的选择 检查这个问题:您可以使用jQuery来实现这一点,但是如何实现:让我解释一下。首先,您需要添加引用,

我有一个HTML页面,我从数据库中包含的数据生成。数据库有时包含浏览器无法中断的长字符串,因为这些字符串不包含可中断字符(空格、点、逗号等)

有没有办法用html、css甚至javascript来解决这个问题


查看此问题的示例。

正如已经多次指出的,不,如果不在显示字符串之前以编程方式预处理字符串,您将无能为力

我知道有一种策略可以在需要的地方插入软连字符(
­;
),但这似乎不是一种流行的选择


检查这个问题:

您可以使用jQuery来实现这一点,但是如何实现:让我解释一下。首先,您需要添加引用,并且有一个插件可以帮助您:但是您需要在获取阶段深入代码。此时,您可以在HttpHandler或Page_PreInit阶段处理这个问题,但不需要任何服务器端代码,这一定很难,或者根本没有办法。我不知道,但您应该能够在数据库获取的html页面中添加一些内容。

有一个特殊字符
&173
­可以做到这一点。
例如:

Dzie­le­nie wy­ra­zów
可以像这样显示:

 1. dzie
 2. le
 3. nie wy
 5. ra
 6. zow

可以,只需将框的css属性设置为:

.some_selector {
    word-wrap: break-word;
}
编辑:一些测试表明,它确实适用于div或p(块级元素),但不适用于表格单元格,也不适用于将div放入表格单元格的情况

IE6IE7IE8Firefox 3.5.3Chrome中测试并运行

作品:

<div style="word-wrap: break-word">aaaaaaaaaaaaaaaaaaaaaaddddddddddddddddddddddddddddddddddddddddddaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa </div>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

我在这里回答我自己的问题

根据您的回答,我提出了这个解决方案(感谢@CMS in的帮助)

此脚本通过在第31个位置插入空格来打断任何长度超过30个字符的单词

以下是固定版本:

我还有一个问题,我宁愿插入一个
­然后是空格。但是分配
节点.nodeValue
节点.textContent
会导致插入文本
­而不是标签

<script type="text/javascript">

    $(function() {
        replaceText(/\w{30}/g, "$& ", document.body);
    });

    function replaceText(oldText, newText, node) {
        node = node || document.body; // base node 

        var childs = node.childNodes, i = 0;

        while (node = childs[i]) {
            if (node.nodeType == 3) { // text node found, do the replacement
                if (node.textContent) {
                    node.textContent = node.textContent.replace(oldText, newText);
                } else { // support to IE
                    node.nodeValue = node.nodeValue.replace(oldText, newText);
                }
            } else { // not a text mode, look forward
                replaceText(oldText, newText, node);
            }
            i++;
        }
    }

</script>

$(函数(){
replaceText(/\w{30}/g,“$&”,document.body);
});
函数replaceText(旧文本、新文本、节点){
node=node | | document.body;//基本节点
var childs=node.childNodes,i=0;
while(node=childs[i]){
如果找到(node.nodeType==3){//text节点,则进行替换
if(node.textContent){
node.textContent=node.textContent.replace(旧文本,新文本);
}else{//对IE的支持
node.nodeValue=node.nodeValue.replace(旧文本,新文本);
}
}否则{//不是文本模式,请向前看
替换文本(旧文本、新文本、节点);
}
i++;
}
}
我会等几天再接受这个答案,以防有人提出更简单的解决方案


谢谢

在将长单词添加到文档之前,将其从文本字符串中拆分更容易

避免孤儿也很好,因为最后一行只有一两个字符

此方法将在长度超过n的每个无空格字符中插入空格, 拆分它,使最后一行至少有min个字符

function breakwords(text, n, min){
 var L= text.length;
 n= n || 20;
 min= min || 2;
 while(L%n && L%n<min)--n;
 var Rx= RegExp('(\\w{'+n+',}?)','g');
 text= text.replace(Rx,'$1 ');
 return text;
}
基于和:“Shy连字符”或“Soft连字符”可以用HTML编写为:
&Shy/
­/
和#xAD
(173 dec=AD hex)。它们都转换为U+00AD字符

DOM文本节点的JavaScript
textContent
nodeValue
不是“实体编码的”——它们只包含实际的实体。因此,为了编写这些字符,您必须自己对它们进行编码:
\xAD
是在JavaScript字符串中编写相同字符的简单方法<代码>字符串。fromCharCode(173)
也可以使用

基于您自己非常好的答案-jQuery插件版本:

$.fn.replaceInText = function(oldText, newText) {
  // contents() gets all child dom nodes -- each lets us operate on them
  this.contents().each(function() {
    if (this.nodeType == 3) { // text node found, do the replacement
        if (this.textContent) {
            this.textContent = this.textContent.replace(oldText, newText);
        } else { // support to IE
            this.nodeValue = this.nodeValue.replace(oldText, newText);
        }
    } else {
      // other types of nodes - scan them for same replace
      $(this).replaceInText(oldText, newText);
    }
  });
  return this;
};

$(function() {
    $('div').replaceInText(/\w{10}/g, "$&\xAD");
});
旁注:

我认为这种情况不应该出现在JavaScript中,而应该出现在服务器端代码中。如果这只是一个用于显示数据的页面,那么在将文本发送到浏览器之前,您可以轻松地对文本执行类似的regexp替换。然而,JavaScript解决方案提供了一个优点(或缺点取决于您想如何看待它)-在脚本执行之前,它不会向数据添加任何无关字符,这意味着任何在HTML输出中抓取数据的机器人都不会看到害羞的连字符。尽管HTML规范将其解释为“断字提示”和一个不可见字符,但它在Unicode世界的其他地方并不能得到保证:(通过我链接的第二篇文章引用Unicode标准)

U+00AD软连字符表示 连字符点,其中换行符 当一个词要表达时,优先使用 连字符。根据剧本, 此文件的可见渲染 发生换行时的字符可能 不同(例如,在某些脚本中) 在中时,它被渲染为连字符- 其他人可能看不见)

另一个注意事项:
在中找到-似乎“”字符
和#8203/
/
U+200b
是您可能希望探索的另一个选项。将
\x20\x0b
作为javascript字符串。

使用
­
我会用inste
$.fn.replaceInText = function(oldText, newText) {
  // contents() gets all child dom nodes -- each lets us operate on them
  this.contents().each(function() {
    if (this.nodeType == 3) { // text node found, do the replacement
        if (this.textContent) {
            this.textContent = this.textContent.replace(oldText, newText);
        } else { // support to IE
            this.nodeValue = this.nodeValue.replace(oldText, newText);
        }
    } else {
      // other types of nodes - scan them for same replace
      $(this).replaceInText(oldText, newText);
    }
  });
  return this;
};

$(function() {
    $('div').replaceInText(/\w{10}/g, "$&\xAD");
});
echo str_replace( "@","<wbr>@", $email );
name.surname
@website.com
.selector_name {
  word-break: break-all;
}

<p class="selector_name">some words some words some words some words</p>

you can obtain:
some word|
s some wo|<-edge of the element
rds some |
words som|
e words  |