Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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
Javascript div rel click函数?_Javascript_Jquery - Fatal编程技术网

Javascript div rel click函数?

Javascript div rel click函数?,javascript,jquery,Javascript,Jquery,我很难为具有相同rel值的多个div创建单击函数 JavaScript: $("[rel=begenici]").click(function () { var postu = $(this).attr(id); alert(postu); }); HTML: 我哪里出错了?你错过了单引号 $("[rel='begenici']").click(function () { var postu = $(this).attr('id'); alert(postu

我很难为具有相同
rel
值的多个div创建单击函数

JavaScript:

$("[rel=begenici]").click(function () {
    var postu = $(this).attr(id);
    alert(postu);
});
HTML:



我哪里出错了?

你错过了单引号

$("[rel='begenici']").click(function () {
    var postu = $(this).attr('id');
    alert(postu);
});

而不仅仅是第一行(谢谢Tushar:)

您需要将属性名用引号括起来

var postu = $(this).attr('id');
完整代码:

$("[rel='begenici']").click(function () {
    var postu = $(this).attr('id');
    alert(postu);
});

使用此
'id'
而不是
id
。并使用
$(“[rel='begnici']”)而不是
$(“[rel=begnici]”)
@Freezystem。在这种情况下,这不是强制性的,但最好使用引号。这也可以在没有引号的情况下使用,但是当属性值包含空格时,就必须使用引号。在我看来,除非您真的知道自己在做什么,否则您应该始终选择安全的方式。Web语言是高度允许的,因此您的代码必须严格@塔萨:我对你的网络技能毫无疑问,但我不能假设你是问问题的。@Freezystem你说得对,这就是为什么我在答案中加了引号:)
$("[rel='begenici']").click(function () {
    var postu = $(this).attr('id');
    alert(postu);
});