suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Kombinieren Sie zwei Selektoren mit einem jQuery-Objekt

Ich habe zwei Divs mit der ID: #addNew_tab#sendCom_tab.

Ich möchte auf eine davon klicken, um dieselbe jQuery-Funktion auszulösen. click()

Ich habe mir so etwas gedacht:

$("#addNew_tab", "#sendCom_tab").click(function(){
      //do stuff
});

Aber das funktioniert nicht.

P粉122932466P粉122932466318 Tage vor361

Antworte allen(2)Ich werde antworten

  • P粉245489391

    P粉2454893912024-03-28 14:55:36

    function doStuff() {
        // do stuff
    }
    
    $("#addNew_tab").click(doStuff);
    $("#sendCom_tab").click(doStuff);

    Antwort
    0
  • P粉769413355

    P粉7694133552024-03-28 14:17:39

    $("#addNew_tab, #sendCom_tab").click(function(){
          //do stuff
    });

    更改自:

    $("#addNew_tab", "#sendCom_tab")

    致:

    $("#addNew_tab, #sendCom_tab")

    选择器内的逗号("a, b") 表示第一个加上第二个;就像 CSS 选择器一样
    (嗯,这是一个 CSS 选择器...)

    它等于:

    $("#addNew_tab").add("#sendCom_tab")...

    Antwort
    0
  • StornierenAntwort