【问题标题】:Get list of elements from DomParser从 DomParser 获取元素列表
【发布时间】:2020-07-13 09:50:28
【问题描述】:

好的,这是我的代码:

<!DOCTYPE html>
<html>

<head>
    <!-- head definitions go here -->
</head>

<body>
    <textarea id="txtArea"></textarea>
    <button id="btn1" onclick="convertHTML()">
        Analyse
    </button>
    <button id="btn2">
        Clear
    </button>
    <div id="output">

    </div>


    <script>
        function clearTxtArea() {

        }
        function convertHTML() {
            var html = document.getElementById('txtArea').value; //Idk of dit werkt lets see
            var doc = new DOMParser().parseFromString(html, "text/xml"); //Zet code om van "<div> fzfhez </div>" etc naar echte HTML elementen
            
            console.log(doc)
            
        }
    </script>
</body>

</html>

基本上我想将 txtArea 中的数据转换为 html 元素(所以输入就像 &lt;a&gt;ono&lt;div&gt;test&lt;/div&gt;&lt;/a&gt; ),我希望我的代码循环遍历所有元素等,并给我一个包含多少个元素的 json 列表以及显示了多少个div等

【问题讨论】:

  • 什么是“json列表”

标签: javascript html domparser


【解决方案1】:

如果您只想计算用户在控制台上方输入的 a 元素,请记录以下代码:

const a = document.querySelectorAll("a");

console.log(a.length);
console.log(a); // element list

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多