This article describes how to implement the shortest XML formatting tool by js. Share it for your reference. The details are as follows:
This is the shortest XML formatting tool with E4X. Unfortunately, IE does not support this standard feature so far...
Please run it in Firefox or Chrome!
1. The code is as follows:
Copy the code as follows: <html>
<head>
<title>Indent XML</title>
<script language="JavaScript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"> </script>
<script language="JavaScript" type="text/javascript;e4x=1">
$(function() {
$('button').click(function() {
$('textarea').val(new XML($('textarea').val()).toXMLString());
});
});
</script>
</head>
<body>
<div>
<textarea rows="8" cols="40"></textarea>
</div>
<div><button>Indent</button></div>
</body>
</html>
2. Before formatting:
3. After formatting:
I hope this article will be helpful to everyone's JavaScript programming.