jQuery 如何使用“如果 $(this) 具有数据属性”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22455466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:56:37  来源:igfitidea点击:

How to use 'If $(this) has data-attribute'

jqueryif-statementcomparisoncustom-data-attribute

提问by Sander Schaeffer

I'm with a little problem. I've got a few buttons, each with a different data-function=""attribute. I want to execute a little script to check which of the buttons has been clicked.

我有一个小问题。我有几个按钮,每个按钮都有不同的data-function=""属性。我想执行一个小脚本来检查哪个按钮被点击了。

<a href="#" class="functionButton" data-function="blogFunctionaliteit">Blog</a>
<a href="#" class="functionButton" data-function="offerteFunctionaliteit">Offerte</a>
<a href="#" class="functionButton" data-function="overzichtFunctionaliteit">Offerte</a>

I simply want a script that says

我只想要一个脚本,上面写着

if $(this) has <data-function="blogFunctionaliteit"> { }
if $(this) has <data-function="offerteFunctionaliteit"> { }
if $(this) has <data-function="overzichtFunctionaliteit"> { }
else { // do nothing }

I've tried lots of thing, but everything doesn't seem to be working, including

我尝试了很多东西,但似乎一切都不起作用,包括

if ($(this).attr('data-function', 'blogFunctionaliteit') { }- Which results in a yes, always, as it only checks if the object has the first parameter, instead of checking them both.

if ($(this).attr('data-function', 'blogFunctionaliteit') { }- 结果是肯定的,总是,因为它只检查对象是否具有第一个参数,而不是同时检查它们。

Thanks in advance for writing the correct jQuery code or could advice me to use something else.

提前感谢您编写正确的 jQuery 代码,或者可以建议我使用其他东西。

回答by ReeCube

For the short way, try this:

简而言之,试试这个:

if ($(this).data("function") === 'blogFunctionaliteit') {
    // TODO: enter your code...
} else if ($(this).data("function") === 'offerteFunctionaliteit') {
    // TODO: enter your code...
} else if ($(this).data("function") === 'overzichtFunctionaliteit') {
    // TODO: enter your code...
} else {
    // do nothing
}

Meanwhile I've earned a lot of experience with JavaScript and I have some improvements for my own code suggestion. If anyone is reading this answer I would highly recommend to use the following code:

同时,我在 JavaScript 方面积累了很多经验,并且对我自己的代码建议进行了一些改进。如果有人正在阅读此答案,我强烈建议您使用以下代码:

var self = this;
var $self = $(self);
var dataFunction = $self.attr('data-function');

switch (dataFunction) {
  case 'blogFunctionaliteit':
    // TODO: enter your code...
    break;
  case 'offerteFunctionaliteit':
    // TODO: enter your code...
    break;
  case 'overzichtFunctionaliteit':
    // TODO: enter your code...
    break;
  default:
    // do nothing
    break;
}

But why the hell you should use this? It's a lot more complicated! Well, yes it is, but here's why:

但是你为什么要使用这个?这要复杂得多!嗯,是的,但原因如下:

Trust me, you will have a less problems with JavaScript if you start writing your code like the second code snippet.

相信我,如果您像第二个代码片段那样开始编写代码,那么 JavaScript 的问题就会少一些。

回答by Felix

You can do:

你可以做:

if ($(this).attr('data-function') == 'blogFunctionaliteit') { }

回答by Furkan Omay

jQuery has native data support: https://api.jquery.com/jQuery.data/

jQuery 有原生数据支持:https: //api.jquery.com/jQuery.data/

$('a').click(function() {
    if($(this).data("function") === "blogFunctionaliteit"){
        alert("You clicked me.");
    };
});

回答by 0__1

Your mistake is that you are using the function attribute in order to apply some changes to the attribute. Instead of that, i think you want to check if the attribute value is the one you want. So this is what you should do:

您的错误是您使用 function 属性是为了对属性应用一些更改。取而代之的是,我认为您想检查属性值是否是您想要的值。所以这是你应该做的:

if ($(this).attr('data-function') === 'blogFunctionaliteit') {
    //bla bla
}