使用javascript从div获取值:始终未定义 [英] Get value from div with javascript: Always get undefined

查看:122
本文介绍了使用javascript从div获取值:始终未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function sync(){
var n1 = document.getElementById('editor')。value;
alert(n1);
var n2 = document.getElementById('news');
n2.value = n1;

div with id 编辑器看起来像这样:

 < div class ='message'id ='editor'contenteditable =trueonkeyUp =' sync()'style =color:black>< / div> 

当我在该div中放入某些东西时,它会提醒我未定义,并且这也会出现在textarea中粘贴也。所以这个问题显然是这样的:

  var n1 = document.getElementById('editor')。value; 

我做错了什么?

解决方案

试试这个

  var n1 = document.getElementById('editor')。innerHTML; //或innerText或textContent 


I had this problem when I get the value from a div:

function sync(){
    var n1 = document.getElementById('editor').value;
    alert(n1);
    var n2 = document.getElementById('news');
    n2.value = n1;
}

div with id editor looks like this:

<div class='message'  id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>

When I put something in that div it will alert me undefined and that will also come in the textarea i paste it in too. So the problem is obviously by this:

var n1 = document.getElementById('editor').value;

What am I doing wrong?

解决方案

Try this

var n1 = document.getElementById('editor').innerHTML; // or innerText, or textContent

这篇关于使用javascript从div获取值:始终未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆