javascript 在 div 中获取子元素?脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22855645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get child elements inside div ? java script
提问by user2734770
<div id="mess">
<h1>Bhashyam School</h1>
<p>AC Nagar Main Road, Nellore <br>Ho, Nellore - 524001 </p>
<p class="tel">Telephone: +(91)-9603444376</p>
</div>
<form name="contactform" method="post" action="send.php" onsubmit="this.divcontent.value = document.getElementById('mess').innerHTML;">
<p>
<input id="box" type="text" name="telephone" placeholder="Enter Your Mobile Number. ex:988*******"><input type="hidden" name="divcontent" id="divcontent" value="" /> <button type="submit" class="button" value="Submit">Get Sms</button>
</p>
</form>
Hello Friends we are developing sms form, i want to grab elements h1, p& p class=telinside of <div id="mess">when form has been submit (onsubmit event)
我们正在开发手机短信的形式朋友你好,我想抓住的元素h1,p与p class=tel里面<div id="mess">的时候形式已经提交(onsubmit事件)
What is the correct javascript code to get element inside div tag elements?
在 div 标签元素中获取元素的正确 javascript 代码是什么?
回答by TheMohanAhuja
Here is a working demo of what you asked
这是您询问的工作演示
What I have done is added string to hidden field divcontentinside form
我所做的是将字符串添加到divcontent表单内的隐藏字段
and updated onsubmitof form with
并更新onsubmit了表格
onsubmit="return getChilds();"
HTML
HTML
<div id="mess">
<h1>Bhashyam School</h1>
<p>AC Nagar Main Road, Nellore <br/>Ho, Nellore - 524001 </p>
<p class="tel">Telephone: +(91)-9603444376 </p>
</div>
<form name="contactform" method="post" action="send.php" onsubmit="return getChilds();">
<p>
<input id="box" type="text" name="telephone" placeholder="Enter Your Mobile Number. ex:988*******"/>
<input type="hidden" name="divcontent" id="divcontent" value="" />
<button type="submit" class="button" value="Submit">Get Sms</button>
</p>
</form>
JavaScript
JavaScript
<script type="text/javascript">
function getChilds()
{
var arr=document.getElementById('mess').childNodes, str='';
for(var i=0; i<arr.length; i++) {
if(arr[i].innerHTML!=undefined) {
str+=" "+(arr[i].textContent||arr[i].innerText);
}
}
document.getElementById("divcontent").value=str;
return true;
}
</script>
better you should have some inputwith type hiddenso hold such data and which is safe and better way of doing this task.
更好的是你应该有一些input类型,hidden所以保存这样的数据,这是完成这项任务的安全和更好的方式。
but why don't you go with jQueryfor this, it offers better and cleaner way for doing many stuffs.
但是为什么不为此使用jQuery,它为做许多事情提供了更好、更干净的方式。
回答by slier
window.onload= function(){
var elems = [];
document.forms[0].onsubmit = function(){
var nodes = document.getElementById('mess').childNodes;
for (var i = 0 ; i < nodes.length; i++){
if (nodes[i].nodeType == 1){
elems.push(nodes[i].innerHTML);
}
}
//console.log(elems);
}
}
elemscontains what u want
elems包含你想要的

