javascript - 动态内容的内容溢出

标签 javascript dynamic overflow

我正在使用内容 div 来显示递归问题的结果 -

从数字 1 开始,重复加 5 或乘以 3,可以产生无限数量的新数字。你会如何编写一个函数,在给定一个数字的情况下,尝试找到产生该数字的加法和乘法序列?例如,数字 13 可以通过先乘以 3 再加上 5 两次来得到,而数字 15 则可以通过以下方式得到: 根本无法联系到。

我正在尝试找到解决方案,然后将其传递回reach()函数,该函数动态填充我的内容div:

<script>
function findTarget(target) {
        function find(start,history) {
            if (start === target) 
                return history;
            else if (start > target) 
                return null;
            else return ((find(start+5,"("+history+"+5)"))||(find(start*3,"("+history+"*3)")));
        }
        return find(1,"1");
    }
    function reach(x) {
       var p=document.createElement('div');
       var result = findTarget(parseInt(x));
       var text = document.createTextNode(result);
       p.appendChild(text);
       p.setAttribute("class","limit");
       document.getElementById('container').appendChild(p);
   }
</script>

<body onLoad="reach(prompt('Enter a target number to reach'));">
    <div id="container"></div>
</body>

这是我的容器和内容 div 的 css

#container{ max-width:500px; margin:0 auto; width:500px; 
background-color:#63DA85; word-break:break-all; }
.limit{ width:300px; max-width:300px; margin:auto; word-break:break-all;}

当我执行这个脚本时,我确实得到了内容 - 但它溢出了 div - enter image description here

我正在尝试使用 word-break:break-all;在我的容器和内容 div 的 css 中 - 但这仅当内容字符串在如下所示的reach函数内生成时才有效

function reach(x) {
       var p=document.createElement('div');
       var text = document.createTextNode("a really long string like pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp");
       p.appendChild(text);
       //p.setAttribute("class","limit");
       document.getElementById('container').appendChild(p);
   }

但是当内容被设置为其他函数(如 findTarget)的返回值时,它就会中断。

我应该如何解决这个问题 - 我不想使用 Jquery - 只是纯 js 解决方案

最佳答案

为此,只需将以下 css 添加到您的 #container

word-break: break-all;

这允许在任意两个字母之间换行,请参阅:

http://jsfiddle.net/us8qpvm0/

关于javascript - 动态内容的内容溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32113828/

相关文章:

javascript - 如果输入值仅包含时间,则初始化 Bootstrap 日期时间选择器时出现 JS 错误

javascript - 您可以使用 WhatsApp API For Business 来验证用户吗?

javascript - 本地存储与 AngularJS $cacheFactory

javascript - 如何使用javascript方法和对象参数生成html

reflection - XML 文件中的 GWT 动态菜单。如何反射(reflect)复合 Material 的类?

c# - 通用类型推断不适用于动态?

go - 了解使用 Go Type Inferred Const 时的溢出

Java,矩阵溢出

javascript - 将文本保留在 div : Both newline, 空白和长单词内

javascript - 在 Javascript 数组中排列一个重复对象以生成一个值