我從 Ajax 呼叫中獲取了一些帶有 div 塊的 html 代碼。我想在這個 html 上構建一個 if/else 檢查,更具體地說是在回傳的 div 的 id 屬性上。如果它小于或大于 10,我會列印不同的結果。
要從 ajax 呼叫回傳的 html:
<div id="10" class="names">Text</div>
<div id="2" class="names">Text</div>
<div id="10" class="names">Text</div>
<div id="11" class="names">Text</div>
Ajax 呼叫
$.ajax({
type: "POST",
url: "https://example.com/api",
data: data,
cache: false,
success: function(html) {
//check html response for the id attribute, print html after the check
},
error: {}
});
知道怎么做嗎?
uj5u.com熱心網友回復:
像這樣的東西?
注意你有兩個 id="10"
const html = `<div id="10" >Text</div>
<div id="2" >Text</div>
<div id="10" >Text</div>
<div id="11" >Text</div>`
function success(html) {
//check html response for the id attribute, print html after the check
const res = $("#content").html(html)
.find("div")
.map(function() {
return $(this).attr("id")
}).get();
console.log(res,res.includes('10') ? 'has id 10' : 'does not have id 10')
}
success(html)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="content"></div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/397080.html
標籤:javascript 查询 阿贾克斯
