Javascript和CSS,使用破折号 [英] Javascript and CSS, using dashes

查看:641
本文介绍了Javascript和CSS,使用破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习一些javascript,并且理解在命名标识符时不允许使用破折号。但是,在CSS中,通常对ID和类使用破折号。

I'm starting to learn some javascript and understand that dashes are not permitted when naming identifiers. However, in CSS it's common to use a dash for IDs and classes.

在CSS中使用破折号是否会干扰javascript交互?例如,如果我使用getElementByID(css-dash-name)。我试过几个例子使用getElementByID作为div ID的名称和破折号,它的工作,但我不知道是否是所有其他上下文的情况。

Does using a dash in CSS interfere with javascript interaction somehow? For instance if I were to use getElementByID("css-dash-name"). I've tried a few examples using getElementByID with dashes as a name for a div ID and it worked, but I'm not sure if that's the case in all other context.

推荐答案

在ID(或类名称,如果你选择)中有破折号和下划线,不会有任何负面影响,可以安全地使用它们。您只是不能执行以下操作:

Having dashes and underscores in the ID (or class name if you select by that) that won't have any negative effect, it's safe to use them. You just can't do something like:

var some-element = document.getElementByID('css-dash-name');

上面的例子会出错,因为你分配的变量中有一个破折号元素。

The above example is going to error out because there is a dash in the variable you're assigning the element to.

以下是很好的,因为变量不包含破折号:

The following would be fine though since the variable doesn't contain a dash:

var someElement = document.getElementByID('css-dash-name');

对于JavaScript变量本身,命名限制

That naming limitation only exists for the javascript variables themselves.

这篇关于Javascript和CSS,使用破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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