12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <header>
- GetElementById Throw
- </header>
- <body>
- <div id="xId">
- <div id="testId">testDiv</div>
- </div>
- <script>
- const getElementById = (parent, id) => {
- try {
- walker(parent, id);
- return null;
- }
- catch (e) {
- if (e.message == "Element found.")
- return e.result;
- else
- throw e;
- }
- }
- function walker(parent, id) {
- let isObject = true;
- if (Array.isArray(parent)) {
- }
- else if (parent != null && typeof parent === "object") {
- if (parent.id == id) {
- let err = new Error("Element found.");
- err.result = parent;
- throw err;
- }
- }
- else
- isObject = false;
- if (isObject) {
- for (child of parent.children) {
- walker(child, id);
- }
- }
- }
- var children = xId.children;
- var val = xId.value;
- el = getElementById(document.body, "testId");
- alert(el.innerText);
- </script>
- </body>
|