在 jquery 选择器中使用 rel 属性中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1322198/
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
using variables in rel attribute in jquery selector
提问by redsquare
I'm using the rel attribute to match a div to a button. I use the button's id in the corresponding div's rel field. There are multiple buttons. When a button is clicked I want to show the corresponding div with the show() method, and to hide the other divs. The buttons work fine, but the divs are not responding. My gut says I'm not formatting the selector properly. Thanks.
我正在使用 rel 属性将 div 与按钮匹配。我在相应 div 的 rel 字段中使用按钮的 id。有多个按钮。单击按钮时,我想使用 show() 方法显示相应的 div,并隐藏其他 div。按钮工作正常,但 div 没有响应。我的直觉说我没有正确格式化选择器。谢谢。
$("div.media_button").click(function (){
var relid = this.id;
$("div.media_button").not(this).fadeTo("normal",0.33);
$(this).fadeTo("normal",1);
$("div.media_selection[rel!='" + relid + "']").hide();
$("div.media_selection[rel='" + relid + "']").show();
});
回答by redsquare
You do not need the single quotes. Can you paste the markup just incase the below doesnt end up working.
您不需要单引号。您能否粘贴标记以防万一下面的内容最终不起作用。
$("div.media_selection[rel=" + relid + "]").hide();
$("div.media_selection[rel=" + relid + "]").show();