JS innerHTML 中的错误值
wrong values with JS innerHTML
我有这个代码:
<div id='html'><input type='text' value='' /></div><br><a onClick="alert(document.getElementById('html').innerHTML);">click me</a>
http://jsfiddle.net/EQ88q/
如果您更改输入值并单击"单击我",您将看不到真正的输入值。是否有任何专有而不是"innerHTML"来获取真正的值?
PS:我使用的是纯JavaScript,我不能使用任何库。
这将起作用,但它非常粗糙:
<div id='html'>
<input type='text' value=''/>
</div>
<script>
function LinkClick()
{
var ele = document.getElementById('html');
ele.children[0].setAttribute("value",ele.children[0].value);
alert(ele.innerHTML);
}
</script>
<br><a onClick="LinkClick()">click me</a>
我看到ComCrure刚刚在评论中提供了这个确切的答案
elem.innerHTML
是元素内部的 HTML 代码。在这种情况下,innerHTML
为空字符串,因为标记内没有内容。
应使用 input_elem.value
获取输入元素的 value
属性。
相关文章:
- 一个ajax循环有两个输出错误innerHTML
- 未捕获的类型错误:无法设置属性'innerHTML'如果为null,则将脚本移动到正文不会;不起作用
- 未捕获的类型错误:无法设置属性'innerHTML'null的:javascript
- Rails和ajax文件上传-无法读取属性'innerHTML'的空错误
- Firefox加载项验证提供了document.write&innerHTML错误
- 未捕获的类型错误:将 innerhtml 设置为数组中的字符串时,无法设置 null 的属性 'innerHTM
- Javascript InnerHTML 输出错误
- JS innerHTML 中的错误值
- 未捕获的语法错误:无法在“元素”上设置“innerHTML”属性:提供的标记是无效的 XML
- document.innerHTML 上的 JavaScript 错误
- 无法设置空错误的属性“innerHTML”
- 未捕获的类型错误:无法读取 Firefox 和 Chrome 中的属性“innerHTML”
- Microsoft JScript运行时错误:无法获取属性'的值;innerHTML':对象为null或
- innerHTML元素位于错误的位置
- 未捕获的类型错误:无法读取属性'innerHTML'的未定义
- 未捕获的类型错误:无法调用方法'innerHTML'即使当我将脚本代码放在<身体>标签
- Extjs4 MVC Xtemplate错误:无法设置属性'innerHTML'为null
- 获取错误:无法设置属性'innerHTML'为null
- 无法设置null错误的innerHTML.尽管存在html ID
- IE显示innerHTML的运行时错误