jQuery selector for element with numeric id using

2019-06-24 03:50发布

问题:

I'm trying to give a red color to a div id='1' class='std' using jQuery (i equals 1):

$("#" + i).css("background-color", "red");

code snippet:

           for(var i=0 ; i< info.length ; i++ ){

                var div_std ='<div id="'+i+'" class="std"> <p> Name :<b> '+info[i].nom_etud + ' </b></p> <hr><p> Abs Hours : '+info[i].hr_cours +'</p>' ;
                div_std+='<p> TPs Hours : '+info[i].hr_tp+'</p><section id="footstd"><button type="button" name="">Mark as absent</button><img src="images/abs.png"></section>'+'</div>';            

                if(info[i].col == 1)
                {
                    $(function() {
                      $("#\\" + i.toString().charCodeAt(0).toString(16)).css("background", "red");
                    });
                }
                else if(info[i].col == 2)
                    $(function() {
                      $("#\\" + i.toString().charCodeAt(0).toString(16)).css("background", "blue");
                    });
                else if(info[i].col == 3)
                    $(function() {
                      $("#\\" + i.toString().charCodeAt(0).toString(16)).css("background", "green");
                    });
                $(".main").append(div_std);   //Display the students name
            }

回答1:

To match ids that start with numbers or special characters you should use CSS escapes:

$(function() {
  // #1 should be escaped as #\31
  $("#\\31").css("background", "red");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="1">div</div>

And this is a lengthier example for ids 1 ... 100:

$(function() {
  var i, selector, $div;
  for (i = 1; i <= 100; i++) {
    selector = "#\\" + i.toString().charCodeAt(0).toString(16) + " " + i.toString().substr(1);
    $div = $('<div id="' + i + '"><code>id: ' + i + ', selector: ' + selector + '</code></div>').appendTo("body");
    $(selector).css("background", "green");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Having said that, in your case you can simply use inline styles to change the background color instead of using CSS selectors:

var bgcolor = ["red", "blue", "green"][info[i].col - 1] || "transparent";
var div_std = '<div id="' + i + '" class="std" style="background-color: ' + bgcolor + '">...'


回答2:

As @D4V1D said, it's impossible to access #1 through CSS, but with jQuery you can—even in a flat way, without div[id=x]. See:

var i = 1;

$('#' + i).css('background-color', 'red');

Try yourself: http://jsfiddle.net/wn7njfuc/

If the solution isn't working for you, probably your jQuery wasn't launched seamlessly.

EDIT 1

According to OP's comment, he is using Jade. So, try this just before your div:

script.
    $(document).ready(function () {
      var i = 1;
      $('#' + i).css('background-color', 'blue');
    });


回答3:

IDs cannot start with a number for # CSS selectors.

Here's how you need to do:

jQuery(function($) {
    $('div[id='+i+']').css("background-color", "red");
});

Edit

My bad, #1 doesn't work in CSS but does with jQuery (as per @GuilhermeOderdenge's answer). As such, there's no need for using $('div[id=1]').