简体   繁体   English

如何使用javascript获取包括所有标签和元素值的html页面

[英]how to obtain html page including all tags and element values using javascript

i use document.documentElement.outerHTML and got everything except it does not contain the current state of all input elements (textbox, checkbox, select, radio buttons etc) 我使用document.documentElement.outerHTML并得到了一切,除了它不包含所有输入元素(文本框,复选框,选择,单选按钮等)的当前状态。

i can use jquery to obtain element values and update the html string. 我可以使用jquery获取元素值并更新html字符串。 but i hope there is a standard way to do this. 但我希望有一种标准的方法可以做到这一点。 any idea? 任何想法?

Here is the code: 这是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
      $(function () {
          $("#me").click(function () {
           alert(  document.documentElement.outerHTML )
          });
      });

</script>

 <style type="text/css">
 </style>

</head>
<body>
 <form>       
<input id="me" type="submit" value="test" />
<input id="Text1" type="text" value="" />
</form>
</body>
</html>

Have you tried this? 你有尝试过吗?

alert($('html').html());

It will alert the whole HTML element except the DOCTYPE . 它将警告除DOCTYPE之外的整个HTML元素

Judging by what you said in your comments, you really just want the key value pairs of the form submission. 根据您在评论中所说的话,您实际上只需要表单提交的键值对。

I would first look into using jQuery's .serialize() 我首先要研究使用jQuery的.serialize()

If jQuery's .serialize() isn't what you are looking for, you should be able to reference each value individually, personally I would first identify your form: 如果jQuery的.serialize()不是您想要的,则您应该能够单独引用每个值,我个人将首先确定您的表单:

<form id="formOfStuff">

Then in pure javascript you can do something like: 然后,在纯JavaScript中,您可以执行以下操作:

var me = formOfStuff.elements["me"].value;
var text1 = formOfStuff.elements["text1"].value;
alert("me=" + me + " text1 = " + text1);

or In jQuery: 或在jQuery中:

var me = $("#me").val();
var text1 = $("#text1").val();
alert("me=" + me + " text1 = " + text1);

The values you entered are part of your POST data and would also be accessible server side on whatever page you are posting to in your form's action. 您输入的值是POST数据的一部分,也可以在表单操作中要发布到的任何页面的服务器端访问。

暂无
暂无

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

相关问题 如何使用JavaScript获取JSON中的所有根元素 - How to obtain all root element in a json using javascript 如何使用 javascript 在页面开头或图像标签之前将标题元素添加到 html 文档中 - How to add a heading element into a html document at the start of the page or before the image tags using javascript 如何获取 html 标签内的内容,包括在 javascript 中使用正则表达式的标签? - How to get content inside html tags including the tags using regex in javascript? 如何使用JavaScript迭代DOM元素中的所有子项(包括那些没有标签的子项) - How to iterate all the children including those without tags in an DOM element with JavaScript 遍历所有html标记,包括Javascript中的子代 - Iterate through all html tags, including children in Javascript JavaScript/jQuery:如何获取 HTML 并显示 HTML,包括标签 - JavaScript/jQuery: how to get HTML and display HTML, including tags 如何在javascript中的html字符串中找到包含标签的html子字符串? - How to find html substring including tags in a html string in javascript? 将javascript包含到html页面 - Including javascript to an html page 如何在node.js上使用cheerio检索所有文本,包括“非中间html”标记? - How to retrieve all texts, including 'non-between' html tags using cheerio on node.js? 如何使用javascript枚举所有自定义HTML标记? - How to enumerate all custom HTML tags using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM