此代码有效:
<script type="text/javascript">
bootbox.alert("Hello world!");
</script>
该代码不起作用:
<script type="text/javascript">
bootbox.alert("Hello
world!");
</script>
如果您要问为什么我像第二个那样需要它,那是因为该值将基于具有多行代码的textarea,所以我需要以某种方式使bootbox运行多行代码。
第二个可以是文本区域的乘积,以Hello
第一行和world!
第二行作为。
将换行符转换\n
为<br>
使用简单的正则表达式:
伪代码:
textAreaObjectText.replace(/\n/g, "<br />");
在g
此,更改所有出现标志是必需的。我们正在使用String.prototype.replace
将已编辑的文本从文本区域发送到警报功能。
工作示例:
document.querySelector("button").addEventListener("click", function(){
bootbox.alert(document.querySelector("textArea").value.replace(/\n/g, "<br />"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="https://github.com/makeusabrew/bootbox/releases/download/v4.4.0/bootbox.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
<textarea>
Hello
World
We're testing
this
alert
</textarea>
<button>open alert</button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句