ID/Class Selector

前端 未结 5 1028
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 20:32

I have some kind of problem with jQuery selectors.

Let\'s say i want to select $(\'#elementID\') but the elementID is a variable.

5条回答
  •  没有蜡笔的小新
    2021-01-14 20:59

    Not really, no. You need "#" as a selector to select an ID. No reason to not use the ID selector. Or you could write your own function, something like:

    $.id = function(id)
    {
        return $("#" + id);
    }
    
    var elementID = "elementID";
    $.id(elementID).text();
    

    That would return an element with the ID of "elementID" without having to use the "#". Kind of pointless though.

提交回复
热议问题