กรองแอตทริบิวต์หลายรายการ

ฉันมีสคริปต์ตัวกรองขนาดเล็กที่แสดงรูปภาพที่ติดแท็ก Alt แต่ฉันยังมี div ที่สามารถค้นหาได้ เพื่อการตรวจสอบที่ถูกต้อง ฉันไม่สามารถให้แท็ก alt แก่พวกเขาได้ ฉันต้องใช้ aria-label

ดังนั้น: ฉันจะค้นหาทั้งแท็ก alt และ aria-label ได้อย่างไร

//Search
$("#search").on("keyup", function () {

  if ($(this).val().length >= 3) {
    // hide everything,
    $(".brand").hide();
    $(".brandsslider").hide();
    // get the text in the input
    var mySelector = $(this).val();
    // show any alt that contains the input
    var myImgs = $("[alt*='" + mySelector + "' i]");
    myImgs.show();
  } else {
    $(".brand").hide();
  }
}); 

person JPC    schedule 06.12.2020    source แหล่งที่มา


คำตอบ (1)


เพียงรวมตัวเลือกของคุณเพื่อกำหนดเป้าหมายแอตทริบิวต์ทั้งสอง

var myImgs = $("[alt*='"+mySelector+"', [aria-label*='"+mySelector+"']");

(หมายเหตุ ฉันลบ stray i ออกจากตัวเลือกเดิมของคุณ เนื่องจากฉันไม่เห็นว่าจะใช้อะไร และอาจทำให้ตัวเลือกแอตทริบิวต์ของคุณล้มเหลว)

person Mitya    schedule 06.12.2020