简体   繁体   English

获取innerHTML,但输入和textarea均为空

[英]get innerHTML but input and textarea are all empty

I have a form with input and textarea fields. 我有一个带有输入和textarea字段的表单。 How can I get the div.innerHTML with the completed fields 如何获取具有完整字段的div.innerHTML

  var foo = $('#div').innerHTML

all fine, but the fields are empty 一切正常,但字段为空

innerHTML is not a property of jquery object. innerHTML不是jquery对象的属性。 You need to use text() or val() 您需要使用text()或val()

See the output in your browser's console of this script. 在此脚本的浏览器控制台中查看输出。 Even if there is just one element selected, jquery returns it in a array, so you was calling the property innerHTML which obviously a javascript array does not have. 即使只选择了一个元素,jquery也会在数组中返回它,因此您正在调用innerHTML属性,而该属性显然是javascript数组所没有的。

 var foo = $('#div').innerHTML; console.log($('#div')); console.log($('#div')[0]); console.log($('#div')[0].innerHTML); console.log($('#div').html()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div">my div innerHTML</div> 

innerHTML is Javascript function whicle $('.class') is jQuery innerHTML是Javascript函数,而$('。class')是jQuery

use 采用

var foo = $('#id').html(); //for html
var foo = $('#id').text(); //for text and not html tags

OR 要么

var foo = document.getElelementById('id').innerHTML;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM