我正在使用内容 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 -
我正在尝试使用 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;
这允许在任意两个字母之间换行,请参阅:
关于javascript - 动态内容的内容溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32113828/