Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery与css混合使用变量、下划线和连字符_Jquery_Css - Fatal编程技术网

jquery与css混合使用变量、下划线和连字符

jquery与css混合使用变量、下划线和连字符,jquery,css,Jquery,Css,我有一个css文件,看起来像这样: .textbox{ position: fixed; width: 225px; } function textbox(name) { ... $(".textbox_" + name).text(); ... $(".textbox-" + name).text(); } 然后我有一个JQuery方法,看起来像这样: .textbox{ position: fixed; width: 225px; } function textbo

我有一个css文件,看起来像这样:

.textbox{
 position: fixed;
 width: 225px;
 }
function textbox(name) {
...
$(".textbox_" + name).text();
...
$(".textbox-" + name).text();
    }
然后我有一个JQuery方法,看起来像这样:

.textbox{
 position: fixed;
 width: 225px;
 }
function textbox(name) {
...
$(".textbox_" + name).text();
...
$(".textbox-" + name).text();
    }

我不知道下划线和连字符做什么,因为css文件中没有下划线或连字符。我真的不明白如何在css中传递变量。

该函数动态地从文本框中获取文本。因此,如果您向该函数传递一个字符串参数,如
“blah”

textbox("blah");
然后是具有类
textbox_blah
textbox blah


如果这些元素确实存在,您可以在类名上使用CSS来设置它们的样式。

让我们假设name==
foo
。如果我理解正确,您正在比较CSS和JavaScript代码,并注意到这些标识符之间的相似性:

  • .textbox
  • .textbox foo
  • .textbox\u foo
所以你想知道:
foo
是某种参数还是与CSS中的
.textbox
类相结合?这是什么关系

答案:没有。
textbox
textbox foo
textbox\u foo
都是完全不同的HTML/CSS类名。连字符和下划线作为CSS中标识符的一部分是完全有效的,因此这三个类的名称不同,就像它们被命名为
abc
def
ghi

因此,没有变量被传递给CSS。没错,CSS没有变量。您所看到的只是具有相似名称的不同类,但就浏览器而言,它们是不相关的

不过,jQuery调用
$(“.textbox_uquo+name)
是JavaScript,您可以在JavaScript中传递变量。使用jQuery,该代码查找一系列具有类名
textbox\u foo
的HTML元素,其中
foo
是传递给
textbox
函数的任何名称。它不会接触类
textbox
textbox foo
的元素,除非它们也有
textbox\u foo


至于JavaScript
textbox
函数,如果看不到它的其余部分,就无法判断它做了什么。根据我见过的其他jQuery插件,我猜想它会根据您提供给它的
name
参数来操作页面中的新文本框或现有文本框。它可能会向它们添加样式和行为,在此过程中,它会使用类
textbox foo
和/或
textbox\u foo
动态添加HTML元素。类似的名称可能有助于您理解生成的HTML源。

您是否有与此对应的HTML?由于
\u
-
在类名中,这意味着还有另一个div叫做
.textbox\u test
或类似的
$(“.textbox-”name.).text()
无法编译,您需要一个
+