jQuery变量到ID的问题 [英] jQuery variable to ID issue

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

问题描述

我有一个名为nav的按钮,当单击按钮时,将读取ID,并将变量"pre"设置为ID.设置完变量后,我将删除部分ID"-link".这部分工作正常,但可能是我的问题所在.一旦删除了"-link",我将尝试使用pre的其余部分来定位页面上div的ID并为其设置动画.因此,我使用另一个变量来一起破解...

I have buttons with a class named nav, when a button is clicked the ID is read and the variable "pre" is set to the ID. Once the variable is set I'm removing part of the id, "-link". This part is working fine but could be where my problem is being created. Once "-link" has been removed I'm trying to use the remainder of pre to target the id of a div on the page and animate it. So I use another variable to hack together...

curr = '$("#'+pre+'")';

这个出现"来创建我需要的东西,但是当我在动画中定位目标对象时,什么也没有发生.大概是b/c我创建了一个字符串,这就是我的知识开始淡出的地方.

This "appears" to create what I need but when I target curr in my animation nothing happens. Probably b/c I have created a string and this is where my knowledge begins to fade.

下面是完整的功能.我计划将其与具有相应div的多个按钮一起使用,因此最好在整个类中使用单个函数.

Below is the complete function. I plan to use this with multiple buttons that have corresponding div's so it would be nice to use a single function for the whole class.

var pre = null;
var curr = null;     
$('.nav').click(function(){
               pre = $(this).attr("id");
               pre = pre.substring(0, pre.indexOf("-link"));
               curr = '$("#'+pre+'")';
               alert(curr);
                curr.animate({opacity:1}, {
                   duration:250,
                   queue:true,
                   complete:function(){
                     //  alert("calling moveOld");
                       moveOld();
                       }
               });

           });

推荐答案

您不需要引号,这就是问题所在.

You don't need the quotes around it, that's the problem.

$('#'+ pre)

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

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