簡體   English   中英

如何使用Xpath javascript獲取innerHTML?

[英]How to get innerHTML with Xpath javascript?

我需要從內部元素中檢索文本。 使用php,使用Xpath非常容易。 在javascript中呢? 我也需要從具有特定ID的特定元素中檢索。 以下代碼中的請求。 可以說CORS沒問題。 我安裝了chrome擴展程序來解決CORS。

<html>
<head>
</head>
<body>


<script>


   fetch('https://alexistogel1.blogspot.com/2019/08/post-tak-penting.html')
    .then(function (response) {
      return response.text();
    })
    .then(function (html) {
      var element = document.evaluate( "//div[@id='post-body-6034358444211110141']" ,document, null, XPathResult.ANY_TYPE, null );
      console.log(element);
    });
</script>
</body>
</html>

使用DOMParser將您的字符串轉換為document
現在,您可以使用documentgetElementById的幫助下選擇其中的元素。

fetch('https://alexistogel1.blogspot.com/2019/08/post-tak-penting.html')
    .then(function (response) {
        return response.text();
    })
    .then(function (text) {
        const parser = new DOMParser();
        return parser.parseFromString(text, 'text/html');
    })
    .then(function (html) {
        var element = html.getElementById('post-body-6034358444211110141');
        console.log(element.innerHTML);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM