jQuery属性选择器变量 [英] jQuery attribute selector variable

查看:124
本文介绍了jQuery属性选择器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个选项的选择框 - 这些选项中的每一个都对应于下面某些图像的值属性。我想要的功能是当selectbox值被更改时,带有相应值的img用红色边框突出显示。以下是代码:

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:

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

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

环顾jquery文档(http://api.jquery.com/attribute-equals-选择器),显然这应该工作...

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

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

Any help would be appreciated, Thank you!

推荐答案

在jQuery 1.7之前

以下内容可行:

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

jQuery 1.7及更高版本

在1.7中,jQuery将语法更改为要求属性在值周围加上引号:

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

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

这篇关于jQuery属性选择器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆