欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
  .html()
  
  描述: 获取集合中第一个匹配元素的HTML内容
  
  这个方法不接收任何元素。
  
  获取集合中第一个匹配元素的HTML内容
  
  在一个 HTML 文档中, 我们可以使用 .html() 方法来获取任意一个元素的内容。 如果选择器匹配多个元素,那么只有第一个匹配元素的 HTML 内容会被获取。 考虑下面的代码:
  
  $('div.demo-container').html();
  
  下文的获取的<div>的内容, 必定是在文档中的第一个class="demo-container"的div中获取的:
  
  <div class="demo-container">
  
  <div class="demo-box">Demonstration Box</div>
  
  </div>
  
  结果如下:
  
  <div class="demo-box">Demonstration Box</div>
  
  这种方法使用浏览器的innerHTML 属性。有些浏览器返回的结果可能不是原始文档的 HTML 源代码。例如,如果属性值只包含字母数字字符,Internet Explorer有时丢弃包裹属性值的引号。
  
  例子:
  
  点击段落将HTML转化为文本
  
  <!DOCTYPE html>
  
  <html>
  
  <head>
  
  <style>
  
  p { margin:8px; font-size:20px; color:blue;
  
  cursor:pointer; }
  
  b { text-decoration:underline; }
  
  button { cursor:pointer; }
  
  </style>
  
  <script src="https://code.jquery.com/jquery-latest.js"></script>
  
  </head>
  
  <body>
  
  <p>
  
  <b>Click</b> to change the <span id="tag">html</span>
  
  </p>
  
  <p>
  
  to a <span id="text">text</span> node.
  
  </p>
  
  <p>
  
  This <button name="nada">button</button> does nothing.
  
  </p>
  
  <script>
  
  $("p").click(function () {
  
  var htmlStr = $(this).html();
  
  $(this).text(htmlStr);
  
  });
  
  </script>
  
  </body>
  
  </html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h63993.shtml