有可能缩短我写的javascript代码吗?
Is it possible to shorten this javascript-code I wrote
我为一个网上商店写了一些javascript代码,但是用了一种新手的方式…
$('div[id^="optionen"] div.wert:nth-child(1) input#format').on("change", function() {
//alert('123');
$('div[id^="optionen"] div.expander').addClass('hidden').css('display','none');
$('div[id^="optionen"] div.expander:nth-child(1)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(2)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(3)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(4)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(5)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(6)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(7)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(8)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(9)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(10)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(11)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(12)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(13)').removeClass('hidden').css('display','block');
});
$('div[id^="optionen"] div.wert:nth-child(2) input#format').on("change", function() {
//alert('123');
$('div[id^="optionen"] div.expander').addClass('hidden').css('display','none');
$('div[id^="optionen"] div.expander:nth-child(1)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(14)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(15)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(16)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(17)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(18)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(19)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(20)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(21)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(22)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(23)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(24)').removeClass('hidden').css('display','block');
$('div[id^="optionen"] div.expander:nth-child(25)').removeClass('hidden').css('display','block');
});
对于另外四个输入,还有四个函数,其中只有第n个子元素的值不同。
我相信可以缩短这段代码?有人能告诉我怎么做吗?
您可能需要查看:nth-child
伪类的选项。您可以针对:nth-child(-n+13)
的前13个孩子。
$('div[id^="optionen"] div.wert:nth-child(1) input#format').on("change", function() {
$('div[id^="optionen"] div.expander')
.addClass('hidden')
.css('display','none');
$('div[id^="optionen"] div.expander:nth-child(-n+13)')
.removeClass('hidden')
.css('display','block')
});
从14到25,你可以使用:nth-of-type(n+14):nth-of-type(-n+25)
您可以简单地遍历您想要更改的项
$('div[id^="optionen"] div.wert:nth-child(1) input#format').on("change", function() {
$('div[id^="optionen"] div.expander').addClass('hidden').css('display','none');
for (var i=1;i<=13;i++) {
$('div[id^="optionen"] div.expander:nth-child(' + i + ')').removeClass('hidden').css('display','block');
}
});
这段代码应该和你发布的完全一样。
var wert = 'div[id^="optionen"] div.wert';
var exp = 'div[id^="optionen"] div.expander';
$(wert + ":nth-child(1) input#format").on("change", function() {
hide($(exp));
for(var i=1;i<=13;i++) {
show($(exp + ":nth-child("+i+")"));
}
}
$(wert + ":nth-child(2) input#format").on("change", function() {
hide($(exp));
show($(exp + ":nth-child(1)"));
for(var i=14;i<=25;i++) {
show($(exp + ":nth-child("+i+")"));
}
}
// FUNTIONS TO HIDE AND SHOW LIKE YOU WANT IT
function show(ctx) {
ctx.addClass("hidden").css("display", "none");
}
function hide(ctx) {
ctx.removeClass("hidden").css("display", "block");
}
我想到的第一件事就是遍历这个:
$('div[id^="optionen"] div.expander:nth-child(**X**)').removeClass('hidden').css('display','block');
因为X是从1到25的唯一变化。
但是,我相信你可以创建一个更好的选择器,这些都在一行中。给我们看看你的HTML,这样我们可以试着帮你。
简短和一般的回答:得到一个数组的jquery对象,你想改变,divs
,可能使用一个选择器与一个共同的类对这些div,或使用.children()
从父元素,但最有意义,然后:
divs.forEach(function(div) {
div.removeClass('hidden').css('display','block');
)};
相关文章:
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 面向对象的Javascript代码在IE7中不起作用
- 通过命令行/批处理文件打开页面时,将javascript代码注入Google Chrome
- JavaScript代码问题:我正在将对象转换为数组
- Javascript阻止其他Javascript代码
- JavaScript代码未正确检查ajax请求
- 如何调试Javascript代码或函数
- 为什么我在这个javaScript代码中使用NaN
- 将javascript代码转换为jquery代码时出错
- 如何从Objective-C代码中调用javascript代码
- 有什么工具可以轻松读取javascript代码吗
- 这个javascript代码是如何编写的
- 如何解密此javascript代码
- SIMPLE Javascript代码,用于显示谷歌电子表格中单个字段的数据
- HTML标记,包含带引号的JavaScript代码中的引号
- 如何在Win8Metro应用程序的Javascript代码中捕获自己的C#事件
- addEventListener的Javascript代码不工作!?(单击时打开放大的img)
- JavaScript代码无法在表单上呈现部分
- 以下 JavaScript 代码与 Facebook 相关
- 使用解析为javascript源的.php,如何使用条件语句将javascript代码封装在php括号之间