亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何選擇axios自帶的元素?

如何選擇axios自帶的元素?

蝴蝶刀刀 2022-12-29 09:49:28
我正在嘗試使用 axios 選擇來自 http 請求的元素const axios = require('axios');axios.get('https://example.com/')    .then((response) => {        console.log(response);    });響應看起來像:<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta name="description" content="example">    <title>Example</title></head><body>    <div class="container">        <div class="row">            <img class="image" src="example.png" alt="example">            <div class="col">Example</div>        </div>    </div></body></html>即我想選擇.image的 src 屬性。我想在response. 如果我們能用 jQuery 做到這一點,那就太好了。我想訪問它的屬性和 html 內容。
查看完整描述

3 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

嘗試使用cheerio(專為服務器設計的核心 jQuery 實現)

axios

   .get(url)

   .then(response => {

     const $ = cheerio.load(response.data);


     const image = $('img')

 

     $("img").each((i, elem) => {

       {

          ...

       })

     ...

   })


查看完整回答
反對 回復 2022-12-29
?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

再見,看這個例子:


console.log($( "div.col" ).text())

console.log($( "img" ).attr('src'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta name="description" content="example">

    <title>Example</title>

</head>

<body>

    <div class="container">

        <div class="row">

            <img src="example.png" alt="example">

            <div class="col">Example</div>

        </div>

    </div>

</body>

</html>

我得到了 div 的內部col文本$( "div.col" ).text()。

要獲得一個屬性(例如) src,img我已經完成了$( "img" ).attr('src')


查看完整回答
反對 回復 2022-12-29
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

如果您將響應添加到文檔,然后嘗試獲取事件,那么您可以嘗試以下方法


$('body').on('click', '.col', function(){

    // do your work

})

如果您想即時選擇任何元素,請使用以下方法


$(response).find('.col')


查看完整回答
反對 回復 2022-12-29
  • 3 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號