马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
div2.children[0].innerHTML = name.value;//undefined
这行代码在运行的时候总是显示undefined
如图
不知但是啥原因,求教啊!</style>
</head>
<body>
<div id="outside">
<h3>最新评论</h3>
<div id="comment">
<div id="comment1" class="comment1">
网友昵称:
<span>李某某</span>
<time>2021/11/22 上午11:46:34</time>
<p>大家好啊!</p>
</div>
</div>
<h4>发表评论</h4>
<div id="addComment">
昵 称:<input type="text" id="name" />
<br /><br />
评论内容:<textarea id="comContent"></textarea>
<button id='tjpl'>提交评论</button>
</div>
</div>
<script>
var div1 = document.getElementById("comment");
var div2 = document.getElementById("comment1").cloneNode(true);
var textarea = document.getElementById("comContent");
var name = document.getElementById("name");
function formatDate(date){
if(!date instanceof Date){
return;
}
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getMinutes();
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
hour = hour < 10 ? '0' + hour : hour;
minute = minute < 10 ? '0' + minute : minute;
second = second < 10 ? '0' + second : second;
return year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
}
var d = new Date();
var dateStr = formatDate(d);
//console.log(dateStr);
var btn = document.getElementById("tjpl");
//console.log(div2.children[0].innerHTML);
//console.log(name);
btn.onclick = function()
{
div1.appendChild(div2);
div2.children[0].innerHTML = name.value;//undefined
div2.children[1].innerHTML = dateStr;
div2.children[2].innerHTML = textarea.value;
}
</script>
var name1 = document.getElementById("name");
div2.children[0].innerHTML = name1.value;
这两处改name 为name1 问题解决 造成的原因可能是name混淆 他不知道是哪个name的Value
</style>
</head>
<body>
<div id="outside">
<h3>最新评论</h3>
<div id="comment">
<div id="comment1" class="comment1">
网友昵称:
<span>李某某</span>
<time>2021/11/22 上午11:46:34</time>
<p>大家好啊!</p>
</div>
</div>
<h4>发表评论</h4>
<div id="addComment">
昵    称:<input id="name" type="text" />
<br /><br />
评论内容:<textarea id="comContent"></textarea>
<button id='tjpl'>提交评论</button>
</div>
</div>
<script>
var div1 = document.getElementById("comment");
var div2 = document.getElementById("comment1").cloneNode(true);
var textarea = document.getElementById("comContent");
var name1 = document.getElementById("name");
function formatDate(date){
if(!date instanceof Date){
return;
}
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getMinutes();
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
hour = hour < 10 ? '0' + hour : hour;
minute = minute < 10 ? '0' + minute : minute;
second = second < 10 ? '0' + second : second;
return year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
}
var d = new Date();
var dateStr = formatDate(d);
var btn = document.getElementById("tjpl");
btn.onclick = function()
{
div1.appendChild(div2);
div2.children[0].innerHTML = name1.value;
div2.children[1].innerHTML = dateStr;
div2.children[2].innerHTML = textarea.value;
}
</script>
|