如何选择具有以数字开头的 ID 的元素? [英] How to select an element that has an ID which begins with a digit?

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

问题描述

我有上面的 HTML(自动生成,无法更改),我尝试在 CSS 中选择整个 DIV.有几个wcustomhtml"在页面上,所以 ID 是我唯一的选择.直截了当,我想.

但我无法选择它,我不明白为什么.

#634670717473476800 和 div#634670717473476800 都无法选择 div.不能选择只由数字组成的ID吗?

那么,如果它是有效的 HTML,那么有什么问题呢?Chrome 的检查元素和 Firebug 都不允许我根据检查器中的 ID 创建规则.

解决方案

您可以通过使用属性选择器并选择 ID 属性来访问它:

[id='634670717473476800'] {背景颜色:红色;}

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

或者通过转义数字:

#\36 34670717473476800 {背景颜色:绿色;}

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

引用自 CSS 字符转义序列:

<块引用>

如果标识符的第一个字符是数字,你需要根据其 Unicode 代码点对其进行转义.例如,代码点因为字符 1 是 U+0031,所以你可以将它转义为 \000031 或\31 .

基本上,要转义任何数字字符,只需在它前面加上 \3 和附加一个空格字符 ( ).是的 Unicode!

ID 属性来自 HTML 规范:

<块引用>

对于 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, and I don't understand why.

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

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

解决方案

You can access it by using an attribute selector and selecting the ID attribute:

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

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

Or by escaping the number:

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

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

Citing from CSS character escape sequences:

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!

The ID attribute from the HTML specification:

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.

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

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