Javascript 如何在jQuery中折射一堆属性赋值?

Javascript 如何在jQuery中折射一堆属性赋值?,javascript,jquery,refactoring,Javascript,Jquery,Refactoring,我有下面一组jQuery代码片段,用于在单击字段时将相应的工具提示分配给特定字段 $('#callerfirst').attr('title', 'May I have your first name?'); $('#callerlast').attr('title', 'May I have your last name?'); $('select[name="carriername"]').attr('title','May I have your wireless prov

我有下面一组jQuery代码片段,用于在单击字段时将相应的工具提示分配给特定字段

$('#callerfirst').attr('title', 'May I have your first name?');
    $('#callerlast').attr('title', 'May I have your last name?');
    $('select[name="carriername"]').attr('title','May I have your wireless provider?');
    $('select[name="csrcallerrelationship"]').attr('title','May I have your relationship to the account holder?');
    $('#accountholder_firstname').attr('title','May I have the first name on the account?');
    $('#accountholder_lastname').attr('title','May I have the last name on the account?');
    $('input[name="contactphone"]').attr('title','May I have a phone number where you can be reached?');
    $('input[name="contactphone2"]').attr('title','May I have a phone number where you can be reached?');
    $('#email').attr('title','With your email address, we will send you an email confirming your claim and another from UPS with your tracking information.');
    $('input[name="address1"]').attr('title','May I have the address on the account?');
    $('select[name="cemake"]').attr('title','May I have the make of your phone?');
    $('select[name="cemodel"]').attr('title','May I have the model of your phone?');
    $('input[name="incidentdate"]').attr('title','May I please have the date on which the incident occured?');
    $('input[name="ccholdername"]').attr('title','May I have the name as it appears on the card?');
    $('input[name="ccmask"]').attr('title','May I have the card number?');
    $('input[name="cvvcode"]').attr('title','May I have the 3 or 4 digit CVV code on the back of the card?');
    $('input[name="billaddress1"]').attr('title','Please provide your billing address.');
    $('input[name="shipaddress1"]').attr('title','May I have the address you want the phone shipped to?');

//Trigger for activating Tooltips
    $('input[title]').tooltip();
    $('select[title]').tooltip({
        events: {
            widget:"focus,blur"
        }
    });

    $('.tooltip').livequery(function() {
        $(this).each(function() {
            $(this).bgiframe();
        });
    });
现在,我如何对上面的代码片段进行折射以获得更好的优化,或者如何减少冗余


如果您有任何帮助,我们将不胜感激。

除了如注释所示将属性移动到HTML标记中之外,您还可以改进代码:

var dictionary = {
    '#callerfirst' : 'May I have your first name?',
    '#callerlast' : 'May I have your last name?',
    'select[name="carriername"]' : 'May I have your wireless provider?',
    'select[name="csrcallerrelationship"]' : 'May I have your relationship to the account holder?'
    ...
    ...
};

$.each(dictionary, function(key, value){
    $(key).attr('title', value);
});

这样做的好处是,如果您需要将文本放在元素中除title属性以外的其他位置,则只需一次更改,而不是多次更改。
attr()
重复被删除。

将文本直接放到标记中每个输入元素的
title
属性中。@VisioN我编辑了问题,很抱歉问题不完整。。请阅读again@Padyster不过,您应该直接在HTML标记中设置属性。你为什么不能?顺便说一句,这些属性值看起来是静态的,所以…工具提示消息会转到popup@Padyster如果您正在寻找优化,那么直接在HTML中设置它。您可以在服务器端动态生成它。你不应该在客户端设置它就是这样,这就是我要找的,非常感谢@MrCode