我有这个jquery代码,根据点击的按钮给出变量 a 和 b 不同的值然后包装文本并提醒它
$(document).ready(function () {
$("button").click(function () {
"andromeda" == this.id ? (a='<‡¦>',b='</‡¦>')
: "milkway" == this.id ? (a='<¬>',b='</¬>')
: "alien-alpha" == this.id ? (a='<¬y>',b='</¬y>')
: "galax" == this.id ? (a='<y×>',b='</y×>')
: "grainlang" == this.id ? (a='<y×>',b='</y×>')
...
: '';
alert(a + 'put here the text to translate into our language. each one corresponds to a different alien dialect' + b);
});
});
和html
<button type="button" id="andromeda">‡¦</button>
<button type="button" id="milkway">¬</button>
<button type="button" id="alien-alpha">¬y</button>
<button type="button" id="galax">y×</button>
<button type="button" id="grainlang">y‡‡</button>
...
关于这一部分,你如何在尽可能小的空间内塞进?我有2120个定义,所以占用的空间很大..是否可以定义一个字典或数组,并根据需要巧妙地转换它们以减少空间?
"andromeda" == this.id ? (a='<‡¦>',b='</‡¦>')
: "milkway" == this.id ? (a='<¬>',b='</¬>')
: "alien-alpha" == this.id ? (a='<¬y>',b='</¬y>')
: "galax" == this.id ? (a='<y×>',b='</y×>')
: "grainlang" == this.id ? (a='<y×>',b='</y×>')
...
: '';
答案 0 :(得分:0)
我认为像这样的东西,有助于在js文件中保存一些代码行。
$("button").click(function () {
var a = '<'+this.innerHTML+'>';
var b = '</'+this.innerHTML+'>';
alert(a + 'put here the text to translate into our language. each one corresponds to a different alien dialect' + b);
});
如果你想将html减少到。我建议您动态生成按钮。您可以按如下方式生成数组:
var array = new Array();
array['andromeda'] = '‡¦';
array['milkway'] = '¬';
array['alien-alpha'] = '¬y';
...
然后迭代生成按钮并将其附加到html。