JQUERY Get Element By parameter ID

后端 未结 6 997
执念已碎
执念已碎 2021-01-20 10:31

just want to know what is the equivalent syntax of this to jquery.

var elm_id = \'my_id\';
var elm = document.getElementById(elm_id);

thank

相关标签:
6条回答
  • I think what you're looking for is

    var elm_id = 'my_id';
    var elm = $('#' + elm_id);
    
    0 讨论(0)
  • 2021-01-20 11:02

    Selecting by ID in jquery is done with the # character:

    $('#my_id')
    

    I invite you to read the documentation about jQuery Selectors

    0 讨论(0)
  • 2021-01-20 11:06
    var elm_id = 'my_id'; 
    var elm = $('#' + elm_id)[0];
    

    Adding the [0] gets the actual node which is what document.getElementById(elm_id); returns.

    0 讨论(0)
  • 2021-01-20 11:10
    var elem_id="my_id";
    $("#"+elem_id);
    
    0 讨论(0)
  • 2021-01-20 11:12

    You need this (I think):

    $('#my_id')
    
    0 讨论(0)
  • 2021-01-20 11:18

    In jquery the following code is being written as:

    var elm = $("#my_id");
    

    OR you can write as:

    var elm_id = 'my_id';
    var elm = $("#" + elm_id);
    
    0 讨论(0)
提交回复
热议问题