Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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/3/wix/2.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 如何将Id定义为变量_Jquery - Fatal编程技术网

Jquery 如何将Id定义为变量

Jquery 如何将Id定义为变量,jquery,Jquery,比方说 <div id = "test_id" class = "test_class"> </div> 我只是写了另一种方法来获得同样的预期结果,但没有奏效,为什么 var test = ' " ' + "test_id" + ' " ' ; alert( $(test).hasClass('test_class') ); // not working, why ????? 现在呢 var test = '#test_id'; // no

比方说

 <div id = "test_id" class = "test_class"> </div>
我只是写了另一种方法来获得同样的预期结果,但没有奏效,为什么

 var test = ' " ' + "test_id" + ' " ' ;

      alert( $(test).hasClass('test_class')  );    // not working, why ?????
现在呢

var test = '#test_id'; // no need for the inner double quotes

alert( $(test).hasClass('test_class') );

变量“test”需要采用ID选择器的格式,即
#test

因此,它应该是:

var test = '#test_id';
alert( $(test).hasClass('test_class') );
你忘了#:


您正在双重引用存储在
测试中的ID

var test = ' " ' + "test_id" + ' " ' ; // test == '"test_id"' == broken
只需执行
var test=“test_id”
,不要忘了在

  • 你忘了“#”
  • “.”不是必需的
  • 你有两个相等的

  • var=test='“+”test_id“+'”;//首先,不能这样分配变量。
    var
    是一个关键字,表示变量声明即将发生。请去掉第一个
    =
    符号

    其次,您在字符串中包含了
    字符。这些用于声明字符串文字,这是在第一个示例中传递到jQuery函数的内容。它们不是字符串本身的一部分

    第三,在第二个示例中,选择器的开头没有包含
    #
    字符

    如果将第二个示例更改为:

    var test = '#test_id';
    

    那么它应该可以正常工作。

    哦,非常感谢你们大家。我愚蠢的错误:(是的,是输入错误(var=test='#test_id')。对此表示抱歉。无论如何,谢谢您的通知
    var test = ' " ' + "test_id" + ' " ' ; // test == '"test_id"' == broken
    
    var test = '#test_id';