使用Jquery/Javascript生成原始Html内容

Gwt Raw Html content using Jquery/Javascript?

本文关键字:原始 Html 内容 Jquery Javascript 使用      更新时间:2023-09-26

我在html页面下面。

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($("#aaa").html());
  });
});
</script>
</head>
<body id="aaa">
<button>hello</button>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<input type="text" name="textt"/>
</body>
</html>

现在点击hello按钮,我需要获得原始的html内容。

它给出了以下输出。

<button>Change content of all p elements</button>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<input name="textt" type="text">

但请注意输出。缺少输入的结束标记。如何在不丢失input的结束标记的情况下获得html的确切原始内容?

我预计产量会低于预期。

<button>Change content of all p elements</button>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<input name="textt" type="text"/>

谢谢!

试试这个:

  $(document).ready(function(){
  $("button").click(function(){
    alert($("#aaa").html() +"</input>");
  });
});