[英]How do I determine if something has been clicked in jQuery on the first load?
I am trying to set a javascript variable to be one of a few options. 我正在尝试将javascript变量设置为几个选项之一。
If one of a few specific link shas not been clicked (ie the first page load), then the variable should be set to 0 or 1 (the default value). 如果未单击某些特定链接中的一个(即,第一页加载),则应将变量设置为0或1(默认值)。
If it has been clicked, then I want it to get a number associated with that link. 如果已单击它,那么我希望它获得与该链接关联的数字。
So say the html looks like this: 所以说html看起来像这样:
<a href=""><img src="icon2.png" id="icon-2"></a> |
<a href=""><img src="icon3.png" id="icon-3"></a> |
<a href=""><img src="icon4.png" id="icon-4"></a> |
The jQuery variable would be something like: jQuery变量类似于:
var icon_num = 2; // If 'icon-2' is clicked, etc.
You could do something like this: 您可以执行以下操作:
var icon_num = 0;
$("#someContainer img").click(function() {
icon_num = parseInt(this.id.replace("icon-",""), 10);
});
Or, give them a data attribute that includes only the ID, like this: 或者,给他们一个仅包含ID的数据属性,如下所示:
<img src="icon2.png" data-id="2" />
This would simplify your code a bit, down to: 这样可以简化您的代码,简化为:
var icon_num = 0;
$("#someContainer img").click(function() {
icon_num = parseInt($(this).attr("data-id"), 10);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.