jQuery 获取this.title属性的前2个字符,并调用对应的id

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

Retrieve first 2 characters of this.title attribute, and call corresponding id

jquery

提问by dylfreak6494

I am trying to retrieve the first two characters of a link's title attribute, store it into a variable, and then call that variable as a function. In the code, where it says "#" + s, is where I want that value to be placed. Any ideas?

我试图检索链接标题属性的前两个字符,将其存储到一个变量中,然后将该变量作为函数调用。在代码中,它说"#" + s,是我想要放置该值的地方。有任何想法吗?

$(".flow").click(function() {
    if (labelstatus = 1) {
        $('.rmflow a,.weflow a,.scflow a,.coflow a,.wcflow a').css({
            color: "#b7b7b7"
        });

        $("." + this.title + " a").css({
            color: "#3e3e3e"
        });

        $("#" + this.title).parent().animate({
            opacity: '1'
        });

        $('.initiate').animate({
            opacity: '.4'
        }, 100);

        // variable to be put in the below selector
        $("#" + s).parent().animate({
            opacity: '1'
        });

        $('.info').fadeOut(200);
        $("#" + this.title).fadeIn(1000);
        return false;
    }
    else {
    }
});

回答by Marius Ilie

get the first 2 characters in the title: var s = $(this).attr("title").substr(0, 2);

获取标题的前 2 个字符: var s = $(this).attr("title").substr(0, 2);

回答by Brent Anderson

Get first two of attribute and find target based on it.

获取属性的前两个并根据它找到目标。

$('.flow').click(function() {
    var first2 = $(this).attr('customatt').substr(0, 2);
    $('#' + first2).doSomething();
    return false;
});