querySelectorAll是否支持id中的period(.)字符? [英] Does querySelectorAll support the period(.) character in an id?

查看:108
本文介绍了querySelectorAll是否支持id中的period(.)字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

querySelectorAll是否支持ID中的句点(.)字符?

Does querySelectorAll support the period(.) character in an id?

我的意思是如果我添加一个像下面这样的元素:

I mean if I append an element like below:

var div = document.createElement('div');
div.id='my.divid';
document.body.appendChild(div);

然后我使用querySelectorAll,如下所示:

and then I use querySelectorAll like below:

document.querySelectorAll('#my.divid');

那我什么也没得到!

因此,id是id的合法字符,而querySelectorAll是Firefox提供的官方方法;我不敢相信该方法不支持id中的period(.).我犯错了吗?

So period is legal character for id, and querySelectorAll is the official method which Firefox provided; I can't believe the method doesn't support period(.) in id. Did I make some mistake?

推荐答案

querySelectorAll中的句点表示您正在指定CSS类.在您的情况下,querySelectorAll试图找出具有id"my"并具有css类"divid"的DOM元素. querySelectorAll如何知道这次您要按id而不是按类来分配元素?拥有适当的id属性是您自己决定的,以免混淆该方法.尽管允许使用句点,但最佳做法是在大多数情况下避免使用该句点,以免混淆jquery等其他库.

A period in querySelectorAll means you are specifying a css class. In your case querySelectorAll is trying to find out a DOM element with id "my" and having a css class "divid". How will querySelectorAll know that this time you want element by id and not by class? It is up to you to have proper id atrribute so as to no to confuse the method. Though a period is allowed, the best practise is to avoid it most of the time so that you do not confuse other libraries like jquery etc.

这篇关于querySelectorAll是否支持id中的period(.)字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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