Javascript jQuery 属性选择器变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6131119/
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-23 20:22:21  来源:igfitidea点击:

jQuery attribute selector variable

javascriptjquery

提问by kinsey

I have a selectbox with several options - each of these option values correspond to a "value" attribute on some images below. The functionality I want is when selectbox value is changed the img with the corresponding value is highlighted with a red border. Here is the code:

我有一个带有多个选项的选择框 - 这些选项值中的每一个都对应于下面某些图像上的“值”属性。我想要的功能是使用红色边框突出显示使用相应值的IMG时。这是代码:

function assignValue() {
  selectboxvalue = $('#Box_style').val() ;
  $('.tabContent img[value="+selectboxvalue+"]').css({border: '1px solid #c10000'});
}

$('#Box_style').change(assignValue);

Looking around at the jquery documentation (http://api.jquery.com/attribute-equals-selector), apparently this should work...

环顾 jquery 文档(http://api.jquery.com/attribute-equals-selector),显然这应该有效......

Any help would be appreciated, Thank you!

任何帮助将不胜感激,谢谢!

回答by c-smile

Prior to jQuery 1.7

jQuery 1.7 之前的版本

The following will work:

以下将起作用:

$('.tabContent img[value='+selectboxvalue+']').css({border: '1px solid #c10000'});

jQuery 1.7 and later

jQuery 1.7 及更高版本

In 1.7 jQuery changed the syntax to require the attributesto have quotation around the value:

在 1.7 中,jQuery 更改了语法以要求attributes在值周围有引号:

$('.tabContent img[value="'+selectboxvalue+'"]').css({border: '1px solid #c10000'});

回答by Pointy

Quotes are messed up:

报价乱了:

  $('.tabContent img[value="'+selectboxvalue+'"]').css({border: '1px solid #c10000'});

回答by Joker82

To the above, it no longer works as mentioned above (, though it might also be only my code). The single quotation messes up the code. The following code works

如上所述,它不再像上面提到的那样工作(尽管它也可能只是我的代码)。单引号弄乱了代码。以下代码有效

As of jQuery 3.2.1 and later

从 jQuery 3.2.1 及更高版本开始

$("#clickmap a[gruppe="+gruppeId+"]").children("path").addClass('lastClicked');