0

ページの段落要素(複数回出現するものもあります)のテキストとして24までのフランス語の数字がいくつかあり、それらを調べて、関連する英語の翻訳をタイトル属性として追加したいと思います。私は解決策を持っていますが、それが最も効率的だとは思わないので、可能であれば、よりきちんとしたものを書くための少しの助けを探しており、あなたの解決策に興味を持っていました。

ありがとう

4

3 に答える 3

4

keyがフランス語の数字で、が英語の翻訳である翻訳オブジェクトを作成しますvalue

var translations = {
    "un" : "one",
    "deux" : "two",
    ...
};
$('.demonstration [class*="col"] p').each(function () {
    var $this = $(this);
    $this.attr('title', translations[$this.text().replace(/\s+/g, '')]);
});
于 2013-02-15T10:25:27.603 に答える
1

代わりに配列とオブジェクトを使用してください。

var myTitles = { vingtquatre : 'twenty four', vingtdeux: 'twenty two' }; // add your other keys and values here in that same format, no matter what the order is inside.

$(this).attr('title', myTitles[$(this).text().replace(/\s+/g, '')]);
于 2013-02-15T10:27:05.163 に答える
0

リストを定義できます。

var nums = { 'un': 'one', 'deux': 'two', ... }

そして、これに置き換えswitchます:

$(this).attr('title', nums[$(this).text().replace(/\s+/g, '')] || null);

|| null番号がリストにない場合、つまり番号のdefault一部のように機能する場合に注意してくださいswitch

于 2013-02-15T10:26:35.143 に答える