我正在使用下面的代码尝试修剪Javascript中的字符串,但我收到标题中提到的错误:
function trim(str) {
return str.replace(/^\s+|\s+$/g,'');
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我解决了这个问题....对不起我应该把代码放在我如何调用它上面....意识到我不小心传递了表单字段本身的对象而不是它的值.
我的以下代码有问题。我想用另一个替换字符串,并生成一个img代码。但是,我收到一条错误消息:str.replace is not a function。知道为什么吗?
<input type="text" id="text">
<input type="button" value="See Code" onclick="myFunction();">
<input type="text" id="code" name="code">
<script>
function myFunction() {
var str = parseInt(document.getElementById("text").value);
var res = str.replace("ftpadress", "htmladress");
var code = str.concat("<img src='",res,"' width='100%'>");
document.getElementById("code").value = code;
}
</script>
Run Code Online (Sandbox Code Playgroud) javascript ×2