简体   繁体   English

使用javascript获取div内的所有标签

[英]Get all tags inside a div using javascript

Newbie in javascript. javascript新手。

Suppose I have this html 假设我有这个html

    <html>
    <div id = "div1">
      <table>
       <tr><td>Name</td></tr>
       <tr><td>Password</td></tr>
      </table>
    </div>
    <div id = "div2">
      <input name="place" value=""/>
      <input name="place" value=""/>
    </div>
    </html>

But I want to retrieve all the tags and elements of a div in "div1". 但是我想在“ div1”中检索div的所有标记和元素。 So elements that will be retrieved will be 因此将要检索的元素将是

          <table>
           <tr><td>Name</td></tr>
           <tr><td>Password</td></tr>
          </table>

Not only the data. 不仅是数据。

如果您在代码中使用此代码,则可以获取您所说的表格

document.getElementById('div1').innerHTML;

First and foremost, congratulation on getting started on JS. 首先,祝贺您开始使用JS。 It's a beautiful scripting language. 这是一种美丽的脚本语言。 Next, I would advise you, to make your life simpler while you should play around with a pure Javascript syntax, it will make your life a whole lot easier if you get started on a library, like jQuery or Mootools . 接下来,我建议您使用纯Javascript语法简化生活,如果您开始使用jQueryMootools之类的库,它将使您的生活变得更加轻松。 JQuery is my preference, mootools is more of a framework. 我更喜欢JQuery,mootools更像是一个框架。

document.getElementById('div1').innerHTML;

in jquery is as elemental as $(#div1) or $(#div1).html() . jquery中的元素与$(#div1)$(#div1).html() Have fun learning! 祝您学习愉快!

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

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