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);
});