我的以下代码有问题。我想用另一个替换字符串,并生成一个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)
正如@mrlew所指出的,
str是的结果,parseInt因此,它是一个数字。replace()是string method,因此不适用于number。
如果我理解正确,那么您想替换一个字符串,检索一个代码,然后使用新的字符串和代码生成一个图像标签。
我会去...
<input type="text" id="text" />
<input type="button" value="See Code" onclick="myFunction();">
<input type="text" id="code" name="code">
<script>
function myFunction() {
//changed
var str = document.getElementById("text").value; //get text
var res = str.replace("ftpadress", "htmladress"); //replace
var code = parseInt(str).toString(); //get code and cast it back to a string
document.getElementById("code").value = code; //insert code
var withTag = code.concat("<img src='", res, "' width='100%'>"); //generate tag
}
</script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10857 次 |
| 最近记录: |