jQuery selectors with variables

前端 未结 5 1058
闹比i
闹比i 2020-11-22 10:24

How to mix variables with selectors?

I have ID variable.

I want to select image with this id from div #one.

jQuery(\'#one img .id\') is

5条回答
  •  隐瞒了意图╮
    2020-11-22 10:50

    Edit: Based on your comment below, you would use this:

    $('#one img.'+id)
    

    In your question you have a space between img and the .class, I've simply removed that so you get img.className or img.'+className

    With the introduction of template literals in ECMAScript 2015, you can also do

    $(`#one img.${id}`)
    

提交回复
热议问题