未捕获的类型错误:无法调用未定义的方法“替换”
Uncaught TypeError: Cannot call method 'replace' of undefined
var container = document.getElementById("f2");
text = text.replace(/':')/g, '<img src="images.jpg" border="0">');
在此代码中,我收到此错误。
在函数的开头text
定义为var
。F2 代码在这里:
<table style='width: 100%'>
<tr>
<td>
<textarea id="fname" class="span5 chattextarea" style="resize: none;" cols="240" rows="13" disabled="disabled" style="float:centre" style="background-color: transparent" style="border: 3px dotted #f7730E;"></textarea>
</td>
</tr>
<tr>
<td>
<input type="text" id="f2" style="width: 440; height:100%" class="chattextarea" autocomplete="off" onkeyup="Javascript: if (event.keyCode==13) testChat();" placeholder= "Type your Message"></input>
<input type="button" class="btn-success" style="width: 50px" value="Send" onclick="testChat()"></input>
</td>
</tr>
</table>
未捕获的类型错误:无法调用未定义的方法"替换">
您尚未在任何地方定义变量text
。
将text.replace()
更改为container.innerHTML.replace()
而且我怀疑你以后想这样做
container.innerHTML = text;
以便您的container
元素获取图像作为新内容。
var container = document.getElementById("f2").innerHTML; // incase you use innerHTML
var text = container.replace(/':')/g, '<img src="images.jpg" border="0">');
如果它是关于元素f2
的值,那么
var container = document.getElementById("f2").value; // incase you use value
var text = container.replace(/':')/g, '<img src="images.jpg" border="0">');
相关文章:
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- AngularJS和promise值在调用本地函数时的效果-未定义
- 为什么这个函数调用会破坏程序并导致未定义的变量
- React路由器错误-'无法调用方法'getRouteAtDepth'的未定义'
- 在不兼容的接收器上调用的方法Set.prototype.add未定义
- PhoneGap无法调用未定义的方法loadurl
- TinyMCE验证给出错误:无法调用方法'getContent'的未定义
- setInterval调用具有未定义参数的函数
- 返回List<字符串>Jquery中的from JSON调用具有未定义的长度
- REST调用总是返回未定义的(Node.JS)
- 如何处理 getJSON 调用上的“未捕获类型错误:无法设置未定义的属性'prop'”
- Javascript/jQuery - “无法调用未定义的方法'push'”,而它是定义的
- 对象不支持在调用 indexOf 定义的数组时 IE8 中未定义的此属性或方法
- 未捕获的类型错误:无法调用方法'至小写'的未定义
- *简单*解析云代码查询失败”;TypeError:无法调用方法'获取'未定义的“检索该信息”;
- 无法调用方法'应用'关于为jquery谷歌地图插件设置方向的未定义
- moment:Array.prototype.some在null或未定义时被调用
- 调用javascript函数时未定义
- 未捕获的类型错误:无法调用方法'每个'的未定义
- Alt flux action:_this.actions未定义,即使调用了this.generateActions