[英]Checking if currently clicked element is equal to a specific div 'id' in JQuery
I have researched on the web and tested for a solution for quite a while, but have not been able to make this work. 我已经在网上进行了研究,并在很长一段时间内测试了一个解决方案,但还是无法完成这项工作。 So I have decided to create a post here, even though its probably low-skilled and I'll get downvoted for it I have put a lot of effort and I hope someone can help out.
所以我决定在这里创建一个帖子,即使它可能技术含量低,而且我会为此付出代价,我付出了很多努力,我希望有人可以提供帮助。
My code at the moment uses .click()
function from JQuery and my goal is to use it on a button so that when I click that button, it checks whether the id
of the button clicked on matches a variable that I set in the .js
file. 我的代码目前使用来自JQuery的
.click()
函数,我的目标是在按钮上使用它,这样当我点击该按钮时,它会检查点击按钮的id
是否与我在中设置的变量匹配.js
文件。 In other words, check if the thing I clicked on's id
is equal to my "target". 换句话说,检查我点击的
id
是否等于我的“目标”。
This is the part of the code with the problem: 这是带有问题的代码的一部分:
var target = '#vig_but_inc';
$(this).click(function() {
if(this.id == target) {
vigor.incStat(); // increase stats once
console.log("hi"); // test if code fires
$('#vig_res').html(vigor.current);
}
});
Please note I have checked if the code works without matching the id
, without the if
-statement - in that situation if I click on a specific, preset button's id
I managed to increase the value of #vig_res
which is my goal. 请注意我已检查代码是否正常工作而没有匹配
id
,没有if
-statement - 在这种情况下,如果我点击特定的预设按钮的id
我设法增加#vig_res
的值,这是我的目标。
However with the if
involved, as explained prior, I am trying to match what is being clicked on with my target
, which is the id
name I wish it to match to. 但是,
if
涉及if
,如前所述, 我正在尝试将正在点击的内容与我的target
匹配 ,这是我希望它匹配的id
名称。 I'm using this
to check my click, which has worked in other areas but doesn't seem to work in this context. 我正在使用
this
来检查我的点击,这在其他方面有效但在这种情况下似乎不起作用。
I have made the variable target global scope as explained here in the comments: Check if the clicked div id matches with variable . 我已经制作了变量目标全局范围,如评论中所述: 检查点击的div id是否与变量匹配 。 It still doesn't work.
它仍然无法正常工作。
I know and have tested $(this).click(..
, where it would fire off the code inside of whatever div
I clicked. So I believe the problem lies afterwards, maybe in the this.id
, but I've also used .is()
like suggested elsewhere but it won't work that way either. 我知道并测试了
$(this).click(..
,它会触发我点击的任何div
内部的代码。所以我相信问题在于之后,也许在this.id
,但我也使用过.is()
就像在其他地方建议的那样,但它也不会那样工作。
I hope someone can give me some insight into how to tackle this problem, thank you. 我希望有人能给我一些解决方法来解决这个问题,谢谢。
The answer is a little more complicated, in the anonymous function which is called when the click is triggered, this
is not the clicked element, it is a jQuery object. 答案有点复杂,在触发点击时调用的匿名函数中,
this
不是单击元素,而是jQuery对象。 You should accept as first parameter event
and use event.target
. 您应该接受第一个参数
event
并使用event.target
。 Plus, target
should not contain the #
, because the .id
returns only the name. 另外,
target
不应包含#
,因为.id
只返回名称。
So, your code should look like this: 所以,你的代码应该是这样的:
var target = 'vig_but_inc';
$(this).click(function(event) {
console.log(event.target);
if(event.target.id == target) {
vigor.incStat(); // increase stats once
console.log("hi"); // test if code fires
$('#vig_res').html(vigor.current);
}
});
Working Fiddle: https://jsfiddle.net/e8np0g9j/3/ 工作小提琴: https : //jsfiddle.net/e8np0g9j/3/
在调用click函数之前使用选择的id和类
$('.class').click(function() {});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.