如何选择一个以数字开头的元素(css)? [英] How to select an element that has id starting with a digit (css)?

查看:159
本文介绍了如何选择一个以数字开头的元素(css)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div id =634670717473476800align =centerstyle =width:100%; overflow-y:hidden; class =wcustomhtml> 

我有上述HTML(自动生成,不能更改),我尝试选择整个DIV在CSS 。在页面上有几个wcustomhtml,所以ID是我唯一的选择。简单的我会想。



但我不能选择它。我不明白。



既没有#634670717473476800也没有div#634670717473476800设法选择div。可以不选择只由数字组成的ID?



编辑:所以如果它是有效的HTML,什么可能是错误的。

解决方案

您可以通过这种方式访问​​它:



CSS:

  id ='634670717473476800'] {
background-color:red;
}

演示



https://jsfiddle.net/h82bvpeh/1/ p>

或转义:



CSS:

 #\36 34670717473476800 {
background-color:green;
}

演示



https://jsfiddle.net/h82bvpeh/2/ p>

引用自 https://mathiasbynens.be/notes/ css-escapes


如果标识符的第一个字符是数字,则需要
它基于其Unicode码点。例如,字符1的代码点
是U + 0031,因此您可以将其转义为\000031或
\31。



基本上,为了转义任何数字字符,只要在它前面加上\3和
后缀一个空格字符()。 Yay Unicode!


关于HTML5 ID http://www.w3.org/TR/html5/single-page.html#the-id-attribute


ID没有其他限制,在
中,ID只能包含数字,以数字开头,以下划线开始
,只包含标点符号等。



<div id="634670717473476800" align="center" style="width: 100%; overflow-y: hidden;" class="wcustomhtml">

I have the above HTML (autogenerated, cannot change) and I try to select the entire DIV in CSS. There are several "wcustomhtml" on the page so the ID is my only choice. Straightforward I would think.

But I am not able to select it. I don't understand.

Neither #634670717473476800 nor div#634670717473476800 manages to select the div. Can one not select an ID consisting of numbers only?

EDIT: So if it is valid HTML, what can be wrong. Neither Chrome inspect element, nor firebug let's me create rules based on the ID in the inspector.

解决方案

You can access it this way:

CSS:

[id='634670717473476800']{
    background-color:red;
}

Demo:

https://jsfiddle.net/h82bvpeh/1/

Or by escaping:

CSS:

#\36 34670717473476800{
    background-color:green;
}

Demo:

https://jsfiddle.net/h82bvpeh/2/

Citing from https://mathiasbynens.be/notes/css-escapes:

If the first character of an identifier is numeric, you’ll need to escape it based on its Unicode code point. For example, the code point for the character 1 is U+0031, so you would escape it as \000031 or \31 .

Basically, to escape any numeric character, just prefix it with \3 and append a space character ( ). Yay Unicode!

About HTML5 id from http://www.w3.org/TR/html5/single-page.html#the-id-attribute:

There are no other restrictions on what form an ID can take; in particular, IDs can consist of just digits, start with a digit, start with an underscore, consist of just punctuation, etc.

这篇关于如何选择一个以数字开头的元素(css)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆